PHP Coding
PHP Coding
Plenty of server software are there on the internet, so you can type which server software you want use it.
In this lab manual we used VETRIGO server. We selected it because it contains application server and MYSQL database server together. Once you
have VERTRIGO installed on your computer, no need of installing additional database server. You can also use XAMP server, it is totally the same.
Introduction to PHP
✓ PHP is a server-side scripting language, which means that the scripts are executed on the server, the computer where the Web site is
located.
✓ This is different than JavaScript, another popular language for dynamic Web sites.
✓ JavaScript is executed by the browser, on the user’s computer. Thus, JavaScript is a client-side language.
✓ Because PHP scripts execute on the server, PHP can dynamically create the HTML code that generates the Web page.
✓ This allows individual users to see customized Web pages.
✓ Web page visitors see the output from scripts, but not the scripts themselves.
Writing PHP code
✓ You add PHP code to your web page by using tags, similar to other tags in the HTML file.
✓ The PHP code section is enclosed in PHP tags with the following form:
<?php
PHP statements
?>
✓ For example, you can add the following PHP section to your HTML file.
<?php
echo “This line brought to you by PHP”;
?>
✓ Web pages that contains PHP should be saved with .php extension.
o Short style
<?
Php statements
?>
To use this tag, you either need to enable short tags in your config file (short_open_tag = On), or compile PHP with short tags
enabled.
o ASP style
<%
Php statements
%>
It can be used if you have enabled the asp_tags configuration setting.
Running the first PHP code
To run, your PHP code must be placed inside the WWW folder of vertigo installation path.
Output of code #2
$name = ”John”;
PHP has a total of eight types: integers, doubles, Booleans, strings, arrays, objects, NULL, and resources:
In programming, type casting refers to converting a variable from one data type to another.
Explicit: Using casting functions to convert one type to another. The are two methods for explicit type conversion.
Operators in php are more similar like operators in other programming languages
Athematic operators
PHP athematic operators code #6 PHP pre and post -increment and decrement operators code #7
1 <?php 1 <?php
2 $num1 = 5; 2 $num1 = 5;
3 $num2 = 10; 4 echo $num++; // post-increment (5)
4 echo $num1 - $num2; // subtraction (-5) 5 echo ++$num; //pre-increment (7)
5 echo $num1 + $num2; //Addition (15) 6 echo --$num ; //pre-decrement (6)
6 echo $num1 * $num2; // multiplication (50) 7 echo $num--;//post-decrement (6)
7 echo $num1 / $num2;//division (0.5) 8 echo $num1; (5)
8 echo $num1 . $num2;// concatenation (510) 9 ?>
9 ?>
PHP if else if… else statement code #12 PHP switch statement code #13
1 <?php 1 <?php
2 $num1 = 5; 2 $num = 3;
3 $num2 =”5”; 4 Switch($num1){
4 if($num1 > $num2) 5 Case 1:
5 echo”num1 is greater than num2”; 6 echo”One”; break;
6 else if($num2>$num1) 7 Case 2:
7 echo “num2 is greater than num2”; 8 echo”Two”;break;
8 else 9 Case 3:
9 echo “num1 and num2 are both equal”; 10 echo “Three”; break;
10 ?> 11 ?>
Loop statements
PHP for statement code #14 PHP while statement code #15
1 <?php 1 <?php
2 for($i = 1; $i<=5;$++){ 2 $i=1;
3 echo $i; 3 while($i<=5) {
4 } 4 echo $i; $i++;
5 }
PHP while statement code #16 PHP nested if statement code #17
1 <?php 1 <?php
2 $i=1; 2 echo”<table border=’1’>”;
3 Do { 3 for($i=1; $i<5; $i++){
4 echo $i; $i++; 4 echo”<tr>”;
5 } while($i<=5) 5 for($j=0;$j<5;$j++){
6 echo “<td>”+($i*$j)+”</td>”;
7 }
8 echo”</tr>”;
9 }
PHP array 10 echo”</table>”;
PHP creating indexed array way 1 code #18 PHP creating indexed array way 2 code #19
1 <?php 1 <?php
2 $product[0] = “Orange”; 2 $product = array(“Orange”, “Avocado”, “Banana”);
3 $product[1] = ”Avocado”; 3 print_r($product);
4 $product[2] = “Banana”; 4 ?>
5 // printing array
6 var_dump($product);
7 ?>
PHP creating associative array way 1 code #20 PHP creating associative array way 2 code #21
1 <?php 1 <?php
2 $product[“p1”] = “Orange”; 2 $product = array(“p1”=>”Orange”, “p2”=>”Avocado”,
3 $product[“p2”] = ”Avocado”; “p3”=>”Banana”);
4 $product[“p3”] = “Banana”; 3 print_r($product);
5 // printing array 4 ?>
6 var_dump($product);
7 ?>
Step 6: Enter field names with their type and length, (if it is integer
ignore the length), then click save
PHP database programming simple insert operation code #28 PHP database programming simple select operation code #29
1 <?php 1 <?php
2 $con = mysql_connect(‘localhost’, ‘root’, ‘vertrigo’); 2 $con = mysql_connect(‘localhost’, ‘root’, ‘vertrigo’);
3 mysql_select_db(‘’SRS’); 3 mysql_select_db(‘’SRS’);
4 $q = “insert into Students values(‘Abebe’, 22, ‘B’)”; 4 $q = “select * from Students”;
5 $result = mysql_query($q); 5 $result = mysql_query($q);
6 if(mysql_affected_rows($result) > 0) 6 while($row = mysql_fetch_array($result)){
7 echo “Inserted successfully”; 7 Echo $row[0], $row[1], $row[2], “<br>”;
8 else 8 }
9 echo “Not inserted”;
Inserting value into Table, from HTML from
Step 2 Writing php file to handle insertion code #31
Step 1 Creating HTML form code #30
// save the file as insert.php
1 <html><head></head>
2 <body> 1 <?php
2 $con = mysql_connect(‘localhost’, ‘root’, ‘vertrigo’);
3 <form method = “GET” action=”insert.php”>
3 mysql_select_db(‘’SRS’);
4 <input type=”text” name=”fname”>
4 // getting values from HTML form
5 <input type=”number” name=”age”>
6 <input type=”text” name=”grade”> 5 $fname = $_GET[“fname”];
6 $age = $_GET[“age”];
7 <input type = “submit” value=”insert”>
7 $grade = $_GET[“grade”];
8 </form>
8 $q = “insert into Students values(‘$fname’, $age, ‘$grade’)”;
9 </body></html>
9 $result = mysql_query($q);
10 if(mysql_affected_rows($result) > 0)
11 echo “Inserted successfully”;
12 else
13 echo “Not inserted”;