11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University
JavaScript generally runs in the browser, or client. PHP runs on the same computer as the website you're visiting (server). It has access to all the information and files on that machine, allows it to: construct custom HTML pages to send to browser, run tasks or perform calculations with data from that website. etc.
PHP is server side programming need to install the "server" to run PHP code. Easiest way to install Web server essentials: Includes: Apache + MySQL + PHP + Perl
By default, PHP documents end with the extension.php. When a web server encounters this extension in a requested file, it automatically passes it to the PHP processor. PHP code are executed on the server, and the result is returned to the browser as plain HTML
PHP codes are placed between the tags Example: My first PHP page
Two types of comments: Single line comment // This is a comment Multiline comment <?php /* This is a section of multiline comments that will not be interpreted */ ?>
In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are NOT case-sensitive. All three echo statements below are legal (and equal): "; echo "Hello World! "; EcHo "Hello World! "; ?>
A semicolon after each statement is mandatory. Without it, PHP treats multiple statements like one statement, which it is unable to understand.
Place a $ in front of all variables This is required to make the PHP parser faster, as it instantly knows whenever it comes across a variable. Example: <?php $mycounter = 1; $mystring = "Hello"; $myarray = array("One", "Two", "Three"); echo $mycounter; echo " "; echo $mystring ; echo " "; echo $myarray[0] ; ?>
Variable names must start with a letter of the alphabet or the _ (underscore) character. Variable names can contain only the characters a-z, A-Z, 0-9, and _ (underscore). Variable names may not contain spaces. Variable names are case-sensitive. PHP is a Loosely Typed Language We do not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value.
PHP has three different variable scopes: Local Local variables are variables that are created within, and can be accessed only by a function. They are generally temporary variables that are used to store partially processed results prior to the function’s return. Global There are cases when you need a variable to have global scope, because you want all your code to be able to access it. Also, some data may be large and complex, and you don’t want to keep passing it as arguments to functions. Static Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable.
Example: $x=5; // global scope function myTest() { $y=10; // local scope echo " Test variables inside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y"; } myTest(); echo " Test variables outside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y";
The global keyword is used to access a global variable from within a function. $x=5; // global scope function myTest() { global $x; $y=10; // local scope echo " Test variables inside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y"; } myTest();
Static variable example: function myTest() { static $x=0; echo $x; $x++; } myTest(); echo " "; myTest(); echo " "; myTest(); echo " "; myTest(); echo " "; myTest();
Enclose each string in either double-quote or single quotes String concatenation uses the period (. ) operator Example: $msgs = 5; $name = "Manto\n"; $name2 = 'manto\n'; echo "$name"; echo '$name2'; echo "You have ". $msgs. " messages."; View source: \n goes here
Two kinds of numeric variables: Non-floating point(integer): $count=18; Floating point: $speed=80.5;
In PHP, the array() function is used to create an array. There are three types of arrays: Indexed arrays - Arrays with a numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays
Two ways to create indexed array: Index can be assigned automatically: $cars=array("Volvo","BMW","Toyota"); Index can be assigned manually: $cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota"; Accessing an array: $cars=array("Volvo","BMW","Toyota"); echo "I like ". $cars[0]. ", ". $cars[1]. " and ". $cars[2]. ".";
Associative arrays are arrays that use named keys There are two ways to create an associative array: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); $age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43"; Accessing an Associative Array: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43") echo "Peter is ". $age['Peter']. " years old.";
A multidimensional array is an array containing one or more arrays Example: NameStockSold Volvo2218 BMW1513 Saab52 Land Rover1715 $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) );
Assignment Same as: $a += $b $a = $a + $b Addition $a -= $b $a = $a - $b Subtraction $a *= $b $a = $a * $b Multiplication $a /= $b $a = $a / $b Division $a %= $b $a = $a % $b Modulus $a.= $b $a = $a. $b String Concatenate $a &= $b $a = $a & $b Bitwise And $a |= $b $a = $a | $b Bitwise Or $a ^= $b $a = $a ^ $b Bitwise Xor $a >= $b $a = $a >> $b Right shift
In PHP we have the following conditional statements: if statement - executes some code only if a specified condition is true if...else statement - executes some code if a condition is true and another code if the condition is false if...elseif....else statement - selects one of several blocks of code to be executed switch statement - selects one of many blocks of code to be executed Very similar to Java !
Example: $jam=date("H"); if($jam<20){ echo "Have a good day!"; } $jam=date("H"); if($jam<20){ echo "Have a good day!"; } else{ echo "Have a good night!"; }
Example: $jam=date("H"); if($jam<10){ echo "Have a good morning!"; } elseif($jam<20){ echo "Have a good day!"; } else{ echo "Have a good night!"; }
Example: $favcolor="red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, or green!"; }
In PHP we have the following conditional statements: The while loop executes a block of code as long as the specified condition is true. The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. The for loop is used when you know in advance how many times the script should run. The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Very similar to Java !
Example: $x=1; while($x "; $x++; } $x=6; do{ echo "The number is: $x "; $x++; }while($x<=5); for ($x=0; $x "; }
Example: $students=array("Anto","Toni","Budi","Wati"); foreach($students as $value){ echo $value." "; }
A function is a block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads. A function will be executed by a call to the function. A function name can start with a letter or underscore (not a number). Function names are NOT case-sensitive.
Example: function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function function writeMsg($name) { echo "Hello $name!"; } writeMsg("Budi"); // call the function
We can add a default value for function’s parameter Example: function setHeight($minheight=50) { echo "The height is : $minheight "; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80);
Function can return a value Example: function sum($x,$y) { $z=$x+$y; return $z; } echo " = ". sum(5,10). " "; echo " = ". sum(7,13). " "; echo "2 + 4 = ". sum(2,4);
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending. PHP array sort functions: sort() - sort arrays in ascending order rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key
Example: $numbers=array(4,6,2,22,11); sort($numbers); $arrlength=count($numbers); for($x=0;$x "; } $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); asort($age); foreach($age as $x=>$x_value){ echo "Key=". $x. ", Value=". $x_value; echo " "; }