Professional Documents
Culture Documents
ENG JOAKIM
UNIVERSITY OF BOSASO
FACULT OF COMPUTER SCIENCE
<?PHP
?>
MySQL
2
LECTURER. ENG : JOAKIM
Chapter One
Introduction.
1) PHP is server scripting language and powerful tool for making dynamic and interactive web
pages.
2) Php was conceived 1994 by Rasmus Lerdorf .
3) Power of php:-
1. It’s easy to learn and use.
2. It’s open source.
3. It’s versatile (is that it is platform independent).
4. It’s well connected with databases.
5. It’s enjoys strong community.
4) Variables :- is a name given storage area to store value.
<?php
$text = ”hellow world” ;
echo $text;
?>
Output is : hellow word
5) Php has three variables scops :-
1. Local variable.
2. Globale variable.
3. Static variable.
Variable with Local scope :- is a variable declared inside the function and can only
be accessed within that function.
<?php
Function myTest (){
$name= “ALI”;
echo $name;
}
myTest();
?>
Variable with Globale scope :- is a variable declared outside the function and can
only be accessed without that function
<?php
$name= “ALI”;
Function myTest (){
// generates error
}
myTest();
echo $name;
?>
7) PHP The static Keyword :- 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.
<?php
Function myTest () {
Static $x = 0 ;
echo $x;
$x++;
}
myTest ();
myTest ();
myTest ();
?>
8) PHP Operators :- Operators are used to perform operations on variables and values.
9) PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
Chapter TWO
Follow control statements.
1) Loops :- are used to execute the same block of code provided the condition remains true again.
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
1. while loop code.
<?php
$i=0;
While ($i<=10){
echo “this nuber is : $i <br> ”;
$i++
}
?>
<?php
$i=0;
Do {
echo “this nuber is : $i <br> ”;
$i++
}
While ($i<=10)
?>
<?php
$marks = array (20, 30, 40, 50);
foreach($marks As $numbers ){
echo “ $numbers <br> ”; // output 20 30 40 50
}
?>
<?php
For( $i = 0; $i <= 10 ; $i++ ){
if ( $i == 4){
break;
}
echo $i; // output 0123
}
?> continue code.
3) php
<?php
For( $i = 0; $i <= 10 ; $i++ ){
if ( $i == 4){
continue;
}
echo $i; // output 0123 5678910
}
?> if…else… elseif statmens.
4) Php
<?php
$x = 4;
if ( $x == 4 ){
echo $x; // output 4
}
?>
2. if… else statements :- executes some code if a condition is true and another code if that condition is
false.
<?php
$x = 4;
if ( $x == 8 ){
echo “ this is equal : $x ”;
}
else {
echo “ this num is equal : $x ”;
}
?>
<?php
$x = 4;
if ( $x == 8 ){
echo $x ;
}
elseif ( $x == 3){
echo $x ;
}
else{
Echo “ this num is equal: $x ”; output is 4
}
?>
e
4. Switch statement :- selects one of many blocks of code to be executed.
<?php
$x = 4;
Switch ($x)
Case “8” :
echo “this num is equal 8 ”;
break;
Case “3” :
echo “this num is equal : 3 ”;
break;
Case “7” :
echo “this num is equal 7 ”;
break;
default;
echo “ this num is equal : $x” ; //output 4
?>
Chapter Three
Arrays
1) Array :- are data container, more than one value can be assigned to single variable.
1. Indexed arrays:- Arrays with a numeric index.
<?php
$arday = array (“ali” , “ ayub” , “xawa” , “anab”) <?php // another you can print array values.
echo $ardayda[0];
echo $ardayda[1]; $arday = array (“ali” , “ ayub” , “xawa” , “anab”)
echo $ardayda[2]; echo $ardayda[0] . $ardayda[1] . $ardayda[2]; $ardayda[3];
echo $ardayda[3];
?>
?>
2. Loop Through an Indexed Array :- print all the values of an indexed array.
<?php
$arday = array (“ali” , “ ayub” , “hawa” , “hamdi”)
for ($i=0; $i<count($arday); $i++){
echo “ $arday[$i] <br> ”;
}
?>
3. PHP Associative Arrays :- arrays that you use named keys that you assign to them.
<?php
$arday = array (‘magaca’=> “ali” , ‘age’=>“20” , ‘gender’ , “male”)
echo “Magaca” . $arday[‘magaca’] . “, Age” . $arday[‘age’] . “, Gender” . $arday[‘gender’] ;
?>
<?php
$arday =array (array ("Sem1",22,18), array("Sem2",15,13) );
echo $arday [0][0].": male: " .$arday [0][1]. ",female: ".$arday [0][2].". ";
echo $arday [1][0] .": male: " .$arday [1][1]. ",female: ".$arday [1][2] .".";
?>
<?php
for ( $row = 0; $row < 4; $row++) {
echo " <p><b> Row number $row </b></p>";
echo " <ul> ";
for ($col = 0; $col < 3; $col++) {
echo " <li> " . $ardayda [$row][$col] . " </li>";
}
echo "</ul>";
}
?>
<?php
$arday = array('name'=>"ali", 'age'=>"12");
echo "first values :- ";
echo $arday['name']." " , $arday['age'] ."<br>";
echo "changed values :- ";
echo $arday['name']="Ahmed". " " ;
echo $arday['age']="20";
?>
Chapter Four
HTML FORMS AND POSTING VARIAEBLES.
1. SIMPLE HTML FORM displays a simple HTML form with two input fields and a submit button:
Html form Output
<html>
<body>
Name:
<form action="#" method="post">
</form>
</body>
</html>
Php code
<?php
if (isset($_POST['sup'])){
$Name = $_POST['name'];
$Email = $_POST['email'];
echo " Your Name is : " . $Name ."<br>";
echo " Your E-mail is : ". $Email ;
}
?>
<html>
<head> OUTPUT
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
if(isset($_POST['sub'])){
$magaca=$_POST['nm'];
$region=$_POST['region'];
$gender=$_POST['gd'];
$language=($_POST['lang'] .",");
echo "Name: $magaca";
echo '<br />';
echo "Region: $region";
echo '<br />';
echo "Gender: $gender";
echo '<br />';
echo "Language: $language";
echo '<br />';
}
?>
</form>
</fieldset>
</body>
</html>