You are on page 1of 10

LECTURER.

ENG JOAKIM
UNIVERSITY OF BOSASO
FACULT OF COMPUTER SCIENCE

PHP AND MySQL


SUMMARY BOOK

<?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;
?>

PHP SUMMARY BOOK BY: AYUUB AK


3
LECTURER. ENG : JOAKIM
6) PHP The global Keyword :- The global keyword is used to access a global variable from within a function.
<?php
$x = 2;
$y= 4
Function myTest () {
global $x , $y;
$y= $x+$y
}
myTest ();
echo $y; // outputs 6
?>

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

PHP SUMMARY BOOK BY: AYUUB AK


4
LECTURER. ENG : JOAKIM

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++
}
?>

2. do..while loop code.

<?php
$i=0;
Do {
echo “this nuber is : $i <br> ”;
$i++
}
While ($i<=10)
?>

3. for loop code.


<?php
For( $i = 0; $i <= 10 ; $i++ ) {
echo “this number is : $i <br> ”;
}
?>

4. foreach loop code.

<?php
$marks = array (20, 30, 40, 50);
foreach($marks As $numbers ){
echo “ $numbers <br> ”; // output 20 30 40 50
}
?>

PHP SUMMARY BOOK BY: AYUUB AK


5
LECTURER. ENG : JOAKIM
2) Php break :- jumbput finish the execution in the current block.

<?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

1. If statements :- executes some code if one condition is true.

<?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 SUMMARY BOOK BY: AYUUB AK


6
LECTURER. ENG : JOAKIM
3. if...elseif...else statement :- executes different codes for more than two conditions.

<?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
?>

PHP SUMMARY BOOK BY: AYUUB AK


7
LECTURER. ENG : JOAKIM

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’] ;
?>

4. PHP - Multidimensional Arrays :- is an array containing one or more arrays.

<?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] .".";
?>

Also you can print like that :-

<?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 SUMMARY BOOK BY: AYUUB AK


8
LECTURER. ENG : JOAKIM
Addin values arrays :-

<?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";
?>

PHP SUMMARY BOOK BY: AYUUB AK


9
LECTURER. ENG : JOAKIM

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">

Name: <input type= "text" name= "name"><br> E-mail:


E-mail: <input type= "text" name= "email"><br>
<input type= "submit" name= "sup" value= “submit”>

</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 ;

}
?>

PHP SUMMARY BOOK BY: AYUUB AK


10
LECTURER. ENG : JOAKIM

<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 />';
}
?>

<fieldset style="width: 330px"><legend>Scholarship Application</legend>


<form action="#" method="post">
<table>
<tr><td>Name</td><td><input type="text" name="nm"></td></tr>
<tr><td>Region</td><td><select name="region">
<option value="bari">Bari</option>
<option value="nugaal">Nuugal</option>
<option value="muduq">Muduq</option>
</select>
</td></tr>

<tr><td>Gender</td><td><input type="radio" name="gd" value="male"> Male


<input type="radio" name="gd" value="female"> Female</td></tr>
<tr><td>Language</td><td>
<input type="checkbox" name="lang[]" value="english">English
<input type="checkbox" name="lang[]" value="somali">Somalia
<input type="checkbox" name="lang[]" value="arabic">Arabic
</td></tr>
<tr>
<td><input type="submit" name="sub" value="Save"> </td>
</tr>
</table>

</form>
</fieldset>
</body>
</html>

PHP SUMMARY BOOK BY: AYUUB AK

You might also like