Session 2 Basics of PHP Web Page Designing

Session 2 Basics of PHP Web Page Designing With Dreamweaver MXSession 11 of 9

Chapter Objectives n n Embedding PHP in HTML Variables and data types Operators Control Structures ASP Session 22 of 24

Embedding PHP in HTML code <? (marks the beginning of PHP script) ………. PHP script code ………………. . ? > (marks the end of PHP script) More HTML code <? more PHP ? > ASP Session 23 of 24

Embedding PHP in HTML(cont. . ) <html> <head> HTML or XHTML <title>PHP Test</title> </head> <body> <? php echo '<p>Hello World</p>'; ? > </body> </html> PHP Code ASP Session 24 of 24

Including code/HTML into your page Include Function Require Function ASP Session 25 of 24

Variables n n n Variables are used for storing a values, like text strings, numbers or arrays. All variables in PHP start with a $ sign symbol. In PHP a variable does not need to be declared before being set. PHP automatically converts the variable to the correct data type, depending on how they are set. Variable Naming Rules l A variable name must start with a letter or an underscore "_" l A variable name can only contain alpha-numeric characters and underscores (a-Z, 0 -9, and _ ) l A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($my. String) ASP Session 26 of 24

Example 1. 2. 3. 4. 5. 6. 7. 8. <? php $var = 'Bob'; $Var = 'Joe'; echo "$var, $Var"; // outputs "Bob, Joe" $4 site = 'not yet'; // invalid; starts with a number $_4 site = 'not yet'; // valid; starts with an underscore $täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228. ? > ASP Session 27 of 24

Assign by Reference n n n n n As of PHP 4, PHP offers another way to assign values to variables: assign by reference. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa. To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs 'My name is Bob' twice: <? $foo = 'Bob'; // Assign the value 'Bob' to $foo $bar = &$foo; // Reference $foo via $bar = "My name is $bar"; // Alter $bar. . . echo $bar; echo $foo; // $foo is altered too. ? > ASP Session 28 of 24

Data Types PHP supports eight primitive types : 1. Four scalar types: ü ü 2. Two compound types: ü ü 3. boolean integer float (floating-point number, aka 'double') string array object And finally two special types: ü ü resource NULL ASP Session 29 of 24

Operators ASP Session 210 of 24

Operators cont… ASP Session 211 of 24

String Operators n There are two string operators: l l The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('. ='), which appends the argument on the right side to the argument on the left side. <? php $a = "Hello "; $b = $a. "World!"; // now $b contains "Hello World!" $a = "Hello "; $a. = "World!"; ? > // now $a contains "Hello World!" ASP Session 212 of 24

Control Structures n n if. . . else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true elseif statement - is used with the if. . . else statement to execute a set of code if one of several condition are true ASP Session 213 of 24

Control Structures (Cont… ) ASP Session 214 of 24

Controls Structures ( Cont … ) n The Switch Statement: l If you want to select one of many blocks of code to be executed, use the Switch statement. l The switch statement is used to avoid long blocks of if. . else code. ASP Session 215 of 24

Control Structures (cont. . ) Looping n n Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this. In PHP we have the following looping statements: l while - loops through a block of code if and as long as a specified condition is true l do. . . while - loops through a block of code once, and then repeats the loop as long as a special condition is true l for - loops through a block of code a specified number of times l foreach - loops through a block of code for each element in an array ASP Session 216 of 24

Control Structures (cont. . ) ASP Session 217 of 24

Control Structures (cont. . ) ASP Session 218 of 24

Control Structures (cont. . ) ASP Session 219 of 24

Functions n n A function is a block of code that can be executed whenever we need it. Creating PHP functions: l All functions start with the word "function()" l Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number) l Add a "{" - The function code starts after the opening curly brace l Insert the function code l Add a "}" - The function is finished by a closing curly brace ASP Session 220 of 24

Example ASP Session 221 of 24

PHP Forms and User Input n n n The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. PHP Form Handling The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts. The PHP $_GET and $_POST variables will be explained in the next slides. ASP Session 222 of 24

PHP $_GET n n The $_GET variable is used to collect values from a form with method="get". The $_GET Variable l l The $_GET variable is an array of variable names and values sent by the HTTP GET method. The $_GET variable is used to collect values from a form 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 it has limits on the amount of information to send (max. 100 characters). ASP Session 223 of 24

Example ASP Session 224 of 24

PHP $_POST n n The $_POST variable is used to collect values from a form with method="post". The $_POST Variable l l The $_POST variable is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form 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. ASP Session 225 of 24

Example ASP Session 226 of 24
- Slides: 26