PHP All
PHP All
PHP PROGRAM
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.
Dont forget the $ sign at the beginning of the variable. In that case it will not work.
<?php
$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 Division Multiplication Modulus Example 10 + 3 10 3 10 / 3 10 * 3 10 % 3 Sample Result 13 7 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
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);
<?php define("YEAR", 2008 ); echo " It is the year ".YEAR; ?> The result: It is the year 2008
Control Structures
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 } ?>
is default";
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
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
Output Using function <html><body> 25 <?php function quadratic() { $a = 5; $result = $a * $a; function echo $result; } echo Using function<br>; quadratic(); function called ?> </body></html>
Example 1:-
Output Using function <html><body> 25 <?php 49 function quadratic($a) { $result = $a * $a; function echo $result <br>; } echo Using function<br>; quadratic(5); function called quadratic(7); function called ?> </body></html>
Example 2:-
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:$list = array(apples, grape, oranges); OR $list = array( 1=> apples, 2=> grape, 3=> oranges);
<html><head><title>Soup!</title></head> <body>Delicious Soups.. Yummy:<br> <?php //create an array $soups = array ( Monday => Mushroom, Tuesday => Chicken Chili, Wednesday => Vegetarian);
//display the array echo <p>$soups</p>;
//display the contents of array print_r ($soups); echo <p>; var_dump ($soups); ?> </body></html>
$list[] = pineapples;
$list[3] = pears; $list[4] = pineapples;
or
Example
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 r+ w Opens the file for reading only. Places the file pointer at the beginning of the files. 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 file
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 file
A new file can be written, or text appended to an existing file, using the PHP fwrite() function.
Example:
//connecting to MySQL $conn = mysql_connect($hostname, $username, $password) OR die(Failed to connect to MySQL!!); //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; ?>
<?php $conn = mysql_connect(localhost, root, ) OR die(Sorry Could not connect to MySQL!!); $r1 = mysql_list_dbs($conn) for($rw = 0; $rw < mysql_num_rows($r1); $rw++) { $thisDB = mysql_tablename($r1, $rw); $list .= <b> . $thisDB . </b><br>; if($thisDB != mysql) { $r2 = mysql_list_tables($thisDB); for($nm=0; $nm < mysql_num_rows($r2); $nm++) $list .= - .mysql_tablename($r2,$nm). <br>; } } echo $list; ?>
Example:
<?php $conn = mysql_connect(localhost, root, ) OR die(Sorry Could not connect to MySQL!!); $r1 = mysql_select_db(dbPelajar, $conn) OR die(Sorry No such database in MySQL!!);
Example:
?>