Académique Documents
Professionnel Documents
Culture Documents
PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the browser.
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. There are two basic statements to output text with PHP: echo and print. Note: The file must have the .php extension. If the file has a .html extension, the PHP code will not be executed.
Comments in PHP
In PHP, use // to make a single-line comment or /* and */ to make a large comment block.
<html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>
PHP VARIABLES
VARIABLES IN PHP
Variables are used for storing a values, like text strings, numbers or arrays. When a variable is set it can be used over and over again in your script All variables in PHP start with a $ sign symbol.
$street = "jln raja musa mahadi"; $city = "ipoh"; $zip = 31400; echo "The address is: <br> $street <br>$city $zip";
?> </body></html>
ARITHMETIC OPERATORS
Operator
+ -
Name
Addition Subtraction
Example
10 + 3 10 3
Sample Result
13 7
/ *
%
Division Multiplication
Modulus
10 / 3 10 * 3
10 % 3
3.33333333333 30
1
returns
Hello World
COMPARISON OPERATORS
Perform comparative tests using their operands and return Boolean value TRUE if the test is successful or FALSE if the test fails. Some of the comparison operators; ==, !=, >, <, >=, <=
Constant
CONSTANT
Variables offer flexible way of storing data because you can change their values and the type of data they store at any time during the execution of scripts. Constant a value that must remain unchanged throughout scripts execution. Using define() function define(constantName, value);
EXAMPLE
<?php define("YEAR", 2008 ); echo " It is the year ".YEAR; ?>
The result: It is the year 2008
Control Structures
CONTROL STRUCTURES
Three (3) types of control structure in programming
Sequence Selection / Conditional
If statement If..else statement Switch statement
Looping
For loop While loop Do..while loop
CONDITIONAL IF STATEMENT
The If keyword is used to perform the basic conditional PHP test to evaluate an expression for a Boolean value. The statement will be executed only when the expression is true
<?php $num = 7; if($num % 2 != 0) { $msg = "$num is an odd number."; echo $msg; } ?>
IF..ELSE STATEMENT
The PHP else keyword can be used with if statement to provide alternative code to execute in the event that the tested expression is found to be false
<?php $num = 7; if($num % 2 != 0) echo "$num is an odd number."; else echo "$num is an even number."; ?>
SWITCH STATEMENT
<?php $num = 2; switch($num) { case 1 : echo "This break; case 2 : echo "This break; case 3 : echo "This break; default: echo "This } ?>
FOR LOOP
<?php $a = 0; $b = 0; for($i = 0; $i < 5; $i++) { $a += 10; $b += 5; } echo "At the end of the loop a = $a and b = $b" ; ?>
WHILE LOOP
<?php $i = 0; $num = 50; while($i < 10) { $num--; $i++; } echo "Loop stopped at $i<br> \$num is now $num" ; ?>
DO WHILE LOOP
<?php $i = 0; $num = 50; do { $num--; $i++; }while($i < 1); echo "Loop stopped at $i<br> \$num is now $num" ; ?>
Functions
FUNCTION
A piece/block of PHP code that can be executed once or many times by the PHP scripts. Function is a subprogram that contains lines of code that will be called many times
EXAMPLE
Syntax:function func_Name(arguments) { lines of code; return (value); }
Example 1
<html><body> Output <?php function quadratic() { Using function $a = 5; 25 $result = $a * $a; function echo $result; } echo Using function<br>; quadratic(); function called ?> </body></html>
Example 2
<html><body> <?php function quadratic($a) { $result = $a * $a; echo $result <br>; } echo Using function<br>; quadratic(5); function called quadratic(7); function called ?> </body></html>
Output
Using function 25 49
function
Example 3
Output
<html><body> Using function <?php 25 function quadratic($a) 49 { $result = $a * $a; function return ($result); } echo Using function<br>; echo quadratic(5); function called echo <br>; echo quadratic(7); function called ?> </body></html>
Code segment in func.php <?php function quadratic($a) { $result = $a * $a; return ($result); } ?> <form action= func.php method = get> Key in one number : <input type = text name=no ><br> <input type = submit value = Calculate> </form> <?php if(isset($_GET[no])== TRUE) { $quadr = quadratic($_GET[no]); echo Quadratic = $quadr ; } ?>
ARRAY
WHAT IS AN ARRAY?
A collection of multiple values assembled into one overriding variable. An array can consist of numbers and/or strings, which allow this one variable to hold exponentially more information than a simple string or number.
CREATING AN ARRAY
Formal method of creating an array is to use the array( ) function. Syntax:-
<html><head><title>Soup!</title></head> <body>Delicious Soups.. Yummy:<br> <?php //create an array $soups = array ( Monday => Mushroom, Tuesday => Chicken Chili, Wednesday => Vegetarian);
$list[] = pineapples;
$list[3] = pears; $list[4] = pineapples;
or
The fastest an easiest way to access all the values of an array is to use foreach loop.
foreach ($array as $key => $value) { echo Key is $key. Value is $value; }
<html><head><title>Soup!</title></head> <body>Soup of the day:<br> <?php //create an array $soups = array ( Monday => Mushroom, Tuesday => Chicken Chili, Wednesday => Vegetarian, Thursday' = Chicken Noodle, Friday = Tomato, Saturday = Cream of Broccoli); //display each key and value of an array foreach($soups as $day => $soup) { echo $day: $soup<br>; }?> </body></html>
DELETING FILES
The PHP unlink() function permanently deletes the files specified as its argument (if it is a valid file name)
File modes can be specified as one of the six options: Mode Purpose
r Opens the file for reading only. Places the file pointer at the beginning of the files.
r+ w
Opens the file for reading and writing. Places the file pointer at the beginning of the file. Opens the file for writing only. Places the file pointer at the beginning of the file and truncates the file to zero length. If the file does not exist this will attempt to create it.
Opens the file for reading and writing. Places the file pointer at the beginning of the file and truncates the file to zero length. If the file does not exist this will attempt to create it. Opens the file for writing only. Places the file pointer at the end of the file. If the file does not exist this will attempt to create it. Opens the file for reading and writing. Places the file pointer at the end of the file. If the file does not exist this will attempt to create it.
w+
a+
READING A FILES
A file can be opened with the fopen() function A file can then be read with a function called fread() A files length can be found using the filesize() function fclose() function used to close file.
WRITING A FILES
A new file can be written, or text appended to an existing file, using the PHP fwrite()function.
//choosing database from MySQL $dbaseName = dbPelajar; $db = mysql_select_db($dbName); if($db == FALSE) echo Failed to used the database $dbaseName; else echo $dbaseName database succeeds to used;
?>
$query = SELECT idPlj, namaPlj FROM tblPelajar; $r = mysql_query($query, $conn); while($rw = mysql_fetch_array($r)) { echo ID : .$rw[idPlj]; echo Nama Pelajar : .$rw[namaPlj] . <br>; }
?>