1.Introduction
- PHP is defined as hypertext preprocessor
- PHP is open source
- This is a server side scripting language.
- PHP can be used to develop dyanamic web pages.
- PHP is executed in the server side.
2. PHP Coding
- Every commands must always ends with semi colon.
- PHP files must be save with .php extension
- PHP coding must be done inside "<?php ?>"
2.1 Hello World
- Echo keyword is used in PHP to output some message for the user.
- Also we can use HTML elements and attributes during the PHP coding
- Please refer the below code examples.
ex 1: <?php
echo "Hello World";
?>
ex 2: <?php
echo "Hello <br><b>World</b>";
?>
2.2 Commenting in PHP
- There are Three Different ways to Commenting in PHP
1. Single line Comments
//This is a Single Line Comment
2.Multi Line Comments
is a
Multi line
Comment */
3. Shell style comment
#This is a Shell style comment.
2.3 Variables
- In PHP variables have to be declared with "$" mark.
- PHP is a case sensitive language.
- There are mainly two types of variables which are Global and Local variables. Local variables are restricted to a function or a class.
- There are some variables reserved to PHP
Server Variables($_SERVER)
ex 3: <?php
$a=10;
$b=5;
$b=5;
$ans=$a+$b;
//only
the output will be the answer
echo $ans;
//output
will be printed with a message
//concatination
symbol in PHP ‘.’ is same as + mark in java or JavaScript
echo ‘The total is’.$ans;
echo $ans.’is the total’;
echo ‘The total is ‘.$ans.’ For your calculation’;
echo $ans.’is the total’;
echo ‘The total is ‘.$ans.’ For your calculation’;
?>
ex 4: <?php
$value=25;
$value=25;
//auto recognize variable type as a integer
$name="Vira 360";
$name="Vira 360";
//auto recognize variable type as String
$value = ($value*8);
echo "$name ";
echo $name , $value;
echo "$name ";
echo $name , $value;
echo "25 * 8 ",$value;
echo "25 * 8 =
$value";
//single
quotations will not call the output
echo
'25 * 8 = $value';
?>
ex5: <?php
echo '<a href='http:/www.google.lk"> Google </a>';
//html tags can be used in single quotations
?>
2.4 Arithmetic Operation
ex 6: <?php
$number1=1009;
$number2=2003;
echo "Add<br>";
echo $number1+$number2 .'<br>';
echo "Subtract<br>";
echo $number1-$number2 .'<br>';
echo "Product<br>";
echo $number1*$number2 .'<br>';
echo "Add<br>";
echo $number1+$number2 .'<br>';
echo "Divition<br>";
echo $number1/$number2 .'<br>';
echo '<a href='http:/www.google.lk"> Google </a>';
//html tags can be used in single quotations
?>
2.4 Arithmetic Operation
ex 6: <?php
$number1=1009;
$number2=2003;
echo "Add<br>";
echo $number1+$number2 .'<br>';
echo "Subtract<br>";
echo $number1-$number2 .'<br>';
echo "Product<br>";
echo $number1*$number2 .'<br>';
echo "Add<br>";
echo $number1+$number2 .'<br>';
echo "Divition<br>";
echo $number1/$number2 .'<br>';
?>
2.5 IF-ELSE
ex 7: <?php
$number1=10;
$number2=20;
if($number1==$number2){
echo "Two number are Equal<br>";
}
else if($number1<$number2){
echo "Number 2 is greater than Number 1 <br>";
}
else{
echo "Number 2 is Less than Number 1 <br>";
}
?>
2.6 Comparison and Logical Operators
1. $a==$b -> equals
2. $a===$b -> equals and same type
3. $a!=$b -> not equal
4. $a<>$b -> not equal
5. $a!==$b -> not identical(not of the same data type)
6. $a<$b -> less than
7. $a>$b -> greater than
8. $a<=$b -> less than or equal
9. $a>=$b -> greater than or equal
1. $a and $b -> ($a && $b)
2. $a or $b -> ($a || $b)
ex 8: <?php
$number1=10;
$number2=20;
if(($number1==$number2)or($number1==$number2))
{
echo "Two number are Equal<br>";
}
?>
$number1=10;
$number2=20;
if($number1==$number2){
echo "Two number are Equal<br>";
}
else if($number1<$number2){
echo "Number 2 is greater than Number 1 <br>";
}
else{
echo "Number 2 is Less than Number 1 <br>";
}
?>
2.6 Comparison and Logical Operators
1. $a==$b -> equals
2. $a===$b -> equals and same type
3. $a!=$b -> not equal
4. $a<>$b -> not equal
5. $a!==$b -> not identical(not of the same data type)
6. $a<$b -> less than
7. $a>$b -> greater than
8. $a<=$b -> less than or equal
9. $a>=$b -> greater than or equal
1. $a and $b -> ($a && $b)
2. $a or $b -> ($a || $b)
ex 8: <?php
$number1=10;
$number2=20;
if(($number1==$number2)or($number1==$number2))
{
echo "Two number are Equal<br>";
}
?>
2.7 Arrays in PHP
ex 9: <?php
$names=array('Mark','John','Bob');
$names=array('Mark','John','Bob');
//mentioned index will be displayed
from the following method
echo $names[0];
echo $names[1];
echo $names[2];
//assigning additional value for the array
$names[3]='july';
echo $names[0];
echo $names[1];
echo $names[2];
//assigning additional value for the array
$names[3]='july';
//so it will display as july
echo $names[3];
//output the full array
echo $names[3];
//output the full array
print_r($names);
//output will be like this //Array([0]=>Mark[1]=>John[2]=>Bob[3]=>July)
//output will be like this //Array([0]=>Mark[1]=>John[2]=>Bob[3]=>July)
$names[4]='patrik';
print_r($names);
//output will be like this
//Array([0]=>Mark[1]=>John[2]=>Bob[3]=>July[4]=>Patrik)
?>
print_r($names);
//output will be like this
//Array([0]=>Mark[1]=>John[2]=>Bob[3]=>July[4]=>Patrik)
?>
2.7.2 Associative Arrays
ex 10: <?php
$names=array('Mark'=>56,'John'=>45,'Bob'=>78);
echo'Weight of Mark is ' .$names['Mark'].'<br>';
print_r($names);
//output will be like this
//Array([Mark]=>56[John]=>45[Bob]=>78)
?>
2.7.3 Multi Dimensional Array
ex 11: <?php
/*
Students
Name Age weight
Mark 15 46
John 13 65
Tom 14 56
*/
$students=array(array('Mark',15,46),array('John',13,65),array('Tom',14,56));
//Following wiil be output the array as
a Matrix
echo $students[0][0].'<br>'; echo $students[0][1].'<br>';
echo $students[0][2].'<br>';
echo $students[1][0].'<br>';
echo $students[1][1].'<br>';
echo $students[1][2].'<br>';
echo $students[2][0].'<br>';
echo $students[2][1].'<br>';
echo $students[2][2].'<br>';
?>
ex 12: <?php
/*
Students
Name Age weight
Mark 15 46
John 13 65
Tom 14 56
*/
$students=array(array('Name'=>'Mark','Age'=>15,'Weight'=>46),
array('Name'=>'John','Age'=>13,'Weight'=>65),
array('Name'=>'Tom','Age'=>14,'Weight'=>56));
echo $students[0]['Name'].'<br>';
echo $students[0]['Age'].'<br>';
echo $students[0]['Weight'].'<br>';
echo $students[1]['Name'].'<br>';
echo $students[1]['Age'].'<br>';
echo $students[1]['Weight'].'<br>';
echo $students[2]['Name'].'<br>';
echo $students[2]['Age'].'<br>';
echo $students[2]['Weight'].'<br>';
?>
2.8 While and Do while
- Loop-piece of code that run again and again until the condition is true
ex 13: <?php
$counter=1;
echo "Hello world";
//1-true code will run without stopping
//0-false
//while(1) or while(0)
while($counter<=10){
echo "Hello world <br>"; or echo 'Hello world'.$counter.'<br>';
$counter++
}
- while loop -> until the condition is true code will be executed.
-
do while -> executes at least ones even if condition is true.
2.9 Do While
- execute the code first then check the condition
ex 14: <?php$counter=1; echo "Hello world";
//1-true code will run without stopping //0-false //while(1) or while(0)
do{
echo "Hello world <br>"; or echo 'Hello world'.$counter.'<br>'; $counter++
}while($counter<=10)
?>
2.10 For Loop
ex 15:
<?php
for(init; condition; increment)
{
statement(S);
}
{
statement(S);
}
?>
ex 16:
<?php
for($counter=1 ;$counter<=10 ;$counter++)
{
echo 'Hi'.$counter.'<br>';
}
?>
ex 17:
for($counter=1 ;$counter<=10 ;$counter++)
{
echo 'Hi'.$counter.'<br>';
}
?>
2.11 PHP Foreach Loop
ex 17:
<?php
$names=array('mark','john','tom','patrik','july')
foreach($names as $name)
{
echo $name .'br';
}
?>
ex 18:
$names=array('mark','john','tom','patrik','july')
foreach($names as $name)
{
echo $name .'br';
}
?>
ex 18:
<?php
/*
Students
Name Age weight
Mark 15 46
John 13 65
Tom 14 56
*/
$students=array(array('Name'=>'Mark',
'Age'=>15,
'Weight'=>46),
array('Name'=>'John',
'Age'=>13,
'Weight'=>65),
array('Name'=>'Tom',
'Age'=>14,
'Weight'=>56));
foreach($students as $student=> $innrerArray)
{
echo '<b>'.$student.''</b><br>';
foreach($innrerArray as $item)
{
echo $item.'<br>';
}
?>
/*
Students
Name Age weight
Mark 15 46
John 13 65
Tom 14 56
*/
$students=array(array('Name'=>'Mark',
'Age'=>15,
'Weight'=>46),
array('Name'=>'John',
'Age'=>13,
'Weight'=>65),
array('Name'=>'Tom',
'Age'=>14,
'Weight'=>56));
foreach($students as $student=> $innrerArray)
{
echo '<b>'.$student.''</b><br>';
foreach($innrerArray as $item)
{
echo $item.'<br>';
}
?>
2.12 Switch
ex 19:
<?php
$number=2;
switch($number)
{
case 1:echo "Number is one";break;
case 2:echo "Number is Two";break;
case 3:echo "Number is Three";break;
default:echo "No entry";
}
?>
2.13 $_GET
$number=2;
switch($number)
{
case 1:echo "Number is one";break;
case 2:echo "Number is Two";break;
case 3:echo "Number is Three";break;
default:echo "No entry";
}
?>
2.13 $_GET
- $_GET -> Super user variable which means a variable provided by PHP .
- $_GET['name'] ->square brackets to get values and index we are expecting.
- The value you through the get method will value passed in the address bar
<?php
echo $_GET["name"]
?>
- localhost/Get.php?s_name=Mark , The name Mark will be displayed in the address bar.
ex 20: Please try to do the following example
1.form.html
<html>
<body>
<h1>Student info</h1>
<form action="Get.php" method="get">
<b>Name :<b><input type="text" name="stu_name"><br>
<b>Age :<b><input type="text" name="stu_age"><br>
<b>Weight :<b><input type="text" name="stu_weight"><br>
<input type="submit">
</form>
</body>
</html>
2. GET.php
<?php
echo 'Name:'.$_GET["stu_name"].'<br>';
echo 'Name:'.$_GET["stu_age"].'<br>';
echo 'Name:'.$_GET["stu_weight"].'<br>';
?>
Using IF for validation
<body>
<h1>Student info</h1>
<form action="Get.php" method="get">
<b>Name :<b><input type="text" name="stu_name"><br>
<b>Age :<b><input type="text" name="stu_age"><br>
<b>Weight :<b><input type="text" name="stu_weight"><br>
<input type="submit">
</form>
</body>
</html>
2. GET.php
<?php
echo 'Name:'.$_GET["stu_name"].'<br>';
echo 'Name:'.$_GET["stu_age"].'<br>';
echo 'Name:'.$_GET["stu_weight"].'<br>';
?>
- so the output will be displayed on the get.php page.Also the results will be displayed on the url.
Using IF for validation
<?php
if(isset($_GET["stu_name"]) && isset($_GET["stu_age"]) && isset($_GET["stu_weight"]))
{
$name=$_GET["stu_name"];
$age=$_GET["stu_age"];
$weight=$_GET["stu_weight"];
if(!empty($name) && !empty($age) && !empty($weight))
{
echo 'Name:'.$_GET["stu_name"].'<br>';
echo 'Name:'.$_GET["stu_age"].'<br>';
echo 'Name:'.$_GET["stu_weight"].'<br>';
}
else{
echo "Please enter all filds";
}
}
?>
2.14 $_POST
- Super global variable.
- Url will not be modified just like in GET.
- passed values not will be displayed in url.
- secure than GET.
1.form.html
<html>
<body>
<h1>Student info</h1>
<form action="Post.php" method="post">
<b>UserName :<b><input type="text" name="username"><br>
<b>Password :<b><input type="password" name="password"><br>
<input type="submit">
</form>
</body>
</html>
2. post.php
<?php
$_POST["username"];
$_POST["password"];
if($POST["username"]=="mark" && $POST["password"]=="pass")
{
echo "Password is correct";
}
else
{
echo "password is incorrect";
}
echo 'User Name :'.$_POST["username"].'<br>Password'.$_POST["password"].'<br>';
?>
2.15 Functions
- block of code designed to perform a particular task.
- Used to increase the readability of program,to perform a task in responsive manner.
ex 22:
<?php
function hello()
{
echo "Hello NSBM<br>";
echo "Hello NSBM<br>";
echo "Hello NSBM<br>";
}
hello();
function hello()
{
echo "Hello NSBM<br>";
echo "Hello NSBM<br>";
echo "Hello NSBM<br>";
}
hello();
//Calling the function
?>
2.15.2 With Arguments
?>
2.15.2 With Arguments
ex 23:
<?php
function add($num1,$num2)
{
echo $num1 + $num2 .'<br>';
}
add(899,1000);
add(99,1000);
add(899,100);
?>
2.15.3 Return value
ex 24:
function add($num1,$num2)
{
echo $num1 + $num2 .'<br>';
}
add(899,1000);
add(99,1000);
add(899,100);
?>
2.15.3 Return value
ex 24:
<?php
function add($num1,$num2)
{
$result=$num1 + $num2 ;
return $result;
}
$add1=add(899,1000);
echo add(899,1000).'<br>';
$add2=add(2000,1100);
echo add(2000,1100).'<br>';
echo $add1 * $add2;
?>
2.15.4 Date and Time Functions
ex 25:
<?php
$date1=date('d/m/Y');
$date2=date('d.m.Y');
$date3=date('d-m-Y');
//D shows the days name
echo $date1;
echo $date2;
echo $date3;
$time=date('H-i-s');
echo $time;
?>
2.15.4 Global Variables and Functions
ex 26:
$date1=date('d/m/Y');
$date2=date('d.m.Y');
$date3=date('d-m-Y');
//D shows the days name
echo $date1;
echo $date2;
echo $date3;
$time=date('H-i-s');
echo $time;
?>
2.15.4 Global Variables and Functions
ex 26:
<?php
$name="Mark";
echo $name;
function showName()
{
global $name;
echo 'The name is '.$name;
}
showname();
?>
$name="Mark";
echo $name;
function showName()
{
global $name;
echo 'The name is '.$name;
}
showname();
?>
2.16 include() &
require()
ex 27:
1.header.php
<html>
<body>
<h1>Programming</h1>
</body>
</html>
<?php
$name = 'Devin';
?>
2.page1.php
<?php
include 'header.php';
$page1 ='we are in page 1';
echo $page1;
echo $name;
?>
3.page2.php
<?php
include 'header.php';
$page2 ='we are in page 2';
echo $page2;
echo $name;
?>
ex 28:
<html>
<body>
<h1>Programming</h1>
</body>
</html>
<?php
$name = 'Devin';
?>
2.page1.php
<?php
include 'header.php';
$page1 ='we are in page 1';
echo $page1;
echo $name;
?>
3.page2.php
<?php
include 'header.php';
$page2 ='we are in page 2';
echo $page2;
echo $name;
?>
- require is same as include
- require keyword responsible if it cannot find mentioned file or php file it will kill your current page.
- You can also include_once('header.php') or require_once().
ex 28:
<?php
include 'header.php';
include 'header.php';
include_once('header.php');
//so when we have used this included page will executes once
$page1 ='we are in page 1';
echo $page1;
?>
include 'header.php';
include 'header.php';
include_once('header.php');
//so when we have used this included page will executes once
$page1 ='we are in page 1';
echo $page1;
?>
- same thing happens to require_once
To be Continued...
Well explained about basics of PHP . Please Continue for advance concepts.
ReplyDelete