1 / 28

Introduction to PHP: A Powerful Web Development Language

PHP is a widely-used open source scripting language that is especially suited for web development. It can be embedded into HTML and provides web developers with server-side programming capabilities. This introduction covers the basics of PHP syntax, variables, constants, operators, data types, and control structures.

vroberts
Télécharger la présentation

Introduction to PHP: A Powerful Web Development Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP HYPERTEXT PREPROCESSOR

  2. PHP Introduction PHP is a recursive acronym for “PHP: Hypertext Preprocessor” -- It is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

  3. PHP Introduction

  4. Server Side Programming • Provides web site developers to utilise resources on the web server • Non-public resources do not require direct access from the clients • Allows web sites to be client agnostic (unless JavaScript is used also) • Most server side programming script is embedded within markup (although does not have to be, sometimes better not to)

  5. PHP - What is it / does it do? • PHP: PHP Hypertext Pre-processor • Programming language that is interpreted and executed on the server • Execution is done before delivering content to the client • Contains a vast library of functionality that programmers can harness • Executes entirely on the server, requiring no specific features from the client

  6. PHP - What is it / does it do? • Static resources such as regular HTML are simply output to the client from the server • Dynamic resources such as PHP scripts are processed on the server prior to being output to the client • PHP has the capability of connecting to many database systems making the entire process transparent to the client Load PHP File Web Page Request PHP Engine – Run Script HTML Response PHP Results

  7. PHP Language Basics • Look at the building blocks of the PHP language • Syntax and structure • Variables, constants and operators • Data types and conversions • Decision making IF and switch • Interacting with the client application (HTML forms)

  8. PHP - Syntax and Structure • PHP is similar to C • All scripts start with <?php and with with ?> • Line separator: ; (semi-colon) • Code block: { //code here } (brace brackets) • White space is generally ignored (not in strings) • Comments are created using: • // single line quote • /* Multiple line block quote */ • Precedence • Enforced using parentheses • E.g. $sum = 5 + 3 * 6; // would equal 23 • $sum = (5 + 3) * 6; // would equal 48

  9. PHP - Variables • Prefixed with a $ • Assign values with = operator • Example: $author = “Trevor Adams”; • No need to define type • Variable names are case sensitive • $author and $Author are different

  10. PHP - Constants • Constants are special variables that cannot be changed • Use them for named items that will not change • Created using a define function • define(‘milestokm’, 1.6); • Used without $ • $km = 5 * milestokm;

  11. PHP - Operators • Standard mathematical operators • +, -, *, / and % (modulus) • String concatenation with a period (.) • $car = “SEAT” . “ Altea”; • echo $car; would output “SEAT Altea” • Basic Boolean comparison with “==” • Using only = will overwrite a variable value • Less than < and greater than > • <= and >= as above but include equality

  12. PHP - Data Types • PHP is not strictly typed • Different to JAVA where all variables are declared • A data type is either text or numeric • PHP decides what type a variable is • PHP can use variables in an appropriate way automatically • E.g. • $vat_rate = 0.175; /* VAT Rate is numeric */ • echo $vat_rate * 100 . “%”; //outputs “17.5%” • $vat_rate is converted to a string for the purpose of the echo statement • Object, Array and unknown also exist as types, Be aware of them but we shall not explore them today

  13. PHP Operators Operators are used to operate on values. There are four classifications of operators: > Arithmetic > Assignment > Comparison > Logical

  14. PHP Conditional Statements • > if statement - use this statement to execute some code only if a specified condition is true • > if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false • > if...elseif....else statement - use this statement to select one of several blocks of code to be executed • > switch statement - use this statement to select one of many blocks of code to be executed

  15. PHP Arrays • > An array variable is a storage area holding a number or text. The problem is, a variable will hold only one value. • > An array is a special variable, which can store multiple values in one single variable.

  16. PHP Arrays • In PHP, there are three kind of arrays: • > Numeric array - An array with a numeric index • > Associative array - An array where each ID key is associated with a value • > Multidimensional array - An array containing one or more arrays

  17. PHP Loops • > while - loops through a block of code while a specified condition is true • > do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true • > for - loops through a block of code a specified number of times • > foreach - loops through a block of code for each element in an array

  18. PHP Loops - While The while loop executes a block of code while a condition is true. The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

  19. PHP Loops - While

  20. PHP Loops – Do ... While The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. The next example defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:

  21. PHP Loops – Do ... While

  22. PHP Loops - For • Parameters: • > init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)‏ • > condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. • > increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)‏

  23. PHP Loops - For The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

  24. PHP Functions A function will be executed by a call to the function. > Give the function a name that reflects what the function does > The function name can start with a letter or underscore (not a number)‏

  25. PHP Forms - $_GET Function • > The built-in $_GET function is used to collect values from a form sent with method="get". • > Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send (max. 100 characters).

  26. PHP Forms - $_GET Function Notice how the URL carries the information after the file name.

  27. PHP Forms - $_POST Function • > The built-in $_POST function is used to collect values from a form sent with method="post". • > Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. • > Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

  28. PHP Forms - $_POST Function And here is what the code of action.php might look like:

More Related