You are on page 1of 108

 

 
 
CHAPTER  ONE    
Server  Side  Scripting  Basics  
Introduction  to  server-­‐side  scripting    
2  

Contents  To  be  Covered      


¡  Server-­‐side  scripting  languages  

¡  Use  Basic  Syntax  

¡  Send  Data  to  the  Web  Browser    

¡  Write  Comments  

¡  Data  Types  and  Utilize  Variables  

¡  Manipulate  Strings,  Date  Time  


ManipulationConstants,Operators,Numbers      

¡   Control  Statements  ,Arrays  ,Functions,  


Working  with  Objects      
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
3  

Web  Application    
¡ Architecture  :  client-­‐server  
architecture  
¡ Client  :  web  browser    
¡ primary  language  for  web  browsers  is  
HTML  (how  the  web  page  is  presented  )  
¡ And  including  more  plug-­‐ins  like  
RealPlayer,  Flash,  and  Shockwave.    
¡ Work  with  XML    
¡ Support  Java  script          
           
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
4  

Web  Application    
¡ Server  :  
¡ almost  all  of  the  work  of  Web  
applications  takes  place  on  the  
server.    
¡  specific  application,  called  a  Web  
server,  will  be  responsible  for  
communicating  with  the  browser.(apache  or  
Internet  information  server)    
¡  A  relational  database  server  stores  
whatever  information  the  application  
requires.    
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
5  

Web  Application    
¡ Finally,  you  need  a  language  to  
broker  requests  between  the  Web  
server  and  the  database  server;    
¡ it  will  also  be  used  to  perform  
programmatic  tasks  on  the  information  
that  comes  to  and  from  the  Web  
server.    
¡ The  Web  server,  programming  language,  
and  database  server  you  use  must  work  
well  with  your  operating  system.    
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
6  

Web  Application    
¡ Operating  System  
¡ Manage  computer  resources    
¡ Server  OS  or  OS    
¡ Different  OS  (windows  ,mac  ,Linux  etc)      
           

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


7  

Web  Application:  Architecture      


           

Middleware    
Web   (PHP,ASP,JSP)   Data  Base  
Server   (MySql  ,Oracle)  
(Apache,  IIS)  

Web  Browser  
(internet  explorer  ,Netscape,Mozila  etc)    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


8  

What  is  PHP  ?  


¡  PHP  stands  for  PHP:  Hypertext  Preprocessor    

¡  PHP  is  a  server  side  scripting  language.  

¡  PHP  is  an  interpreted  language,i.e.there  is  no  need  


for  compilation.  

¡  PHP  is  an  object-­‐oriented  language.  

¡  PHP  is  an  open-­‐source  scripting  language.  

¡  PHP  is  simple  and  easy  to  learn  language.  

¡  PHP  supports  many  databases  (MySQL,  Informix,  Oracle,  


Sybase,  Solid,  PostgreSQL,  Generic  ODBC,  etc.)      
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
9  

PHP  Features  

¡ Performance:    
¡  Script  written  in  PHP  executes  much  faster  
then  those  scripts  written  in  other  
languages  such  as  JSP  &  ASP.  

¡ Open  Source  Software:  


¡  PHP  source  code  is  free  available  on  the  
web,  you  can  developed  all  the  version  of  
PHP  according  to  your  requirement  without  
paying  any  cost.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10  

PHP  Features  
¡ Platform  Independent:  
¡   PHP  are  available  for  WINDOWS,  MAC,  LINUX  
&  UNIX  operating  system.  A  PHP  application  
developed  in  one  OS  can  be  easily  executed  
in  other  OS  also.  

¡ Compatibility:  
¡   PHP  is  compatible  with  almost  all  local  
servers  used  today  like  Apache,  IIS  etc.  

¡ Embedded:    
¡  PHP  code  can  be  easily  embedded  within  HTML  
tags  and  script.    
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
How  to  run  php  file  on  XAMPP   11  
server?  
XAMPP   start  Apache  
installation   and  MYSQL  from   Install  Notepad++
XAMPP

the  file  in  XAMP  installation  directory  or  web   write  php  program  or  
root   directory.   Default   XAMP   installation  
directory  for  windows  is  C/xampp   code  on  text  editor.

In  the  xampp  directory  there  is  htdocs  


directory  open  the  directory  and  create  
folder  and  give  any  name.

 save  all  php  file  to  the  folder  you  are  


creating  with  .php  extension  name.

In  your  browser  address  bar,  type  the  address:    


http:/localhost/folder_name/filename.php  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
12  

Server  Side  Scripting  Language  ?  


¡  A  typical  web  server  today  contains  four  
elements  in  addition  to  the  physical  
hardware.  

¡  LAMP  (Linux,  Apache,  MySQL,  and  PHP)    


WAMP  (Windows,  Apache,  MySQL,  &  PHP)  

¡  XAMPP  (Apache,  MariaDB  ,  Perl  and  PHP)  


¡  PHP  
¡  ASP  
¡  Perl  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


13  

Basic  Syntax    
 
Tag  style   Starting  tag   Ending  tag  
Standard   <?php   ?>  
Short  hand   <?   ?>  
Script   <script  language=“php”>   </script>  

php  can  be  saved  .php  or  embedded  into  html.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


14  

Basic  Syntax    
¡  PHP  code  is  executed  on  the  server,  and  the  
plain  HTML  result  is  sent  to  the  browser  
¡  create  a  file  and  write  HTML  tags  +  PHP  code  
and  save  this  file  with  .php  extension.    
¡  Each  code  line  in  PHP  must  end  with  a  
semicolon  (;)  
¡  <?php      //your  code  here    ?>    or    
<!DOCTYPE>      
<html>      
<body>  
         <?php    echo  "<h2>Hello  World!</h2>”;  ?>    
 </body>    
 </html>          
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
15  

Send  output  to  the  browser?    


¡  echo;  

¡  print();  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


16  

PHP  echo  /Print    


¡ Printing  string  
           <?php    echo  "Hello  world";    ?>      
               Output:  Hello  world    
¡ Printing  multi  line  string  File:  
           <?php      
   echo  "Hello  by  PHP  echo                                        
this  is  multi  line    text    ";    ?>      
Output:  Hello  by  PHP  echo  this  is  multi  line  
text    
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
17  

PHP  echo  

¡ Printing  variable  value:    


<?php      
$msg="Hello  world";      
echo  "Message  is:  $msg";          
?>      
             Output:  Message  is:  Hello  world    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


18  

PHP  Comments    
¡  Purpose  :  
¡  To  provide  information  /readable  code/  
¡  To  hide  codes  
There  are  two  ways:      
¡ single  line  comments    
¡  //  (C++  style  single  line  comment)  
¡  #  (Unix  Shell  style  single  line  
comment)    
¡ Multiple  line  comments  
¡  enclose  all  lines  within  /*    */        

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


19  

Example:  PHP  comments    


<?php  
//single  line  comment    
echo  “  hello  world!”;  
/*  Multiple  line    
Comment  */     Output:  hello  
world!    
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


20  

 Errors  
¡ Notice:    
¡  Message  for  What  you  should  do  and  what  you  
should  not  do    
Possible  causes  :  
¡  accessing  a  variable  that  has  not  yet  been  
defined.  
<?php  
$a="Defined  error";  
echo  "Notice  error";  
echo  $b;  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


21  

 Errors  
¡ Warning:    
¡  Occurs  at  run  time    
¡  Do  not  interrupt  code  execution    
   possible  Causes  :  
¡  Calling  on  an  external  file  that  does  not  
exist  in  the  directory  
¡  Wrong  parameters  in  a  function  
 <?php  
echo  "Warning  error"';  
include  ("external_file.php");  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


22  

 Errors  
¡ Fatal  Error:      
¡  Occurs  at  run  time    
¡  Program  execution  is  not  continued  it  
terminates  
¡  They  crash  your  program  (critical  errors)  
Possible  causes  :    
¡  An  undefined  function  or  class  in  the  
script  is  the  main  reason  for  this  type  of  
error.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


23  

 Errors  
¡  For  instance,  the  following  script  would  
result  in  a  fatal  error:  
<?php  
function  sub()  
{  
$sub=6-­‐1;  
echo  "The  sub=  ".$sub;  
}  
div();  
?>  

   
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
24  

 Errors  
Parse  Error  (Syntax)  
¡  Parse  errors  are  caused  by  misused  or  missing  
symbols  in  a  syntax.    

¡  The  compiler  catches  the  error  and  terminates  


the  script.  
Possible  causes:  
¡  Unclosed  brackets  or  quotes  
¡  Missing  or  extra  semicolons  or  parentheses  
¡  Misspellings  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


25  

 Errors  
¡  For  example,  the  following  script  would  stop  
execution  and  signal  a  parse  error:  
 
<?php  
       echo  "Red";  
       echo  "Blue";  
       echo  "Green"  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


26  

PHP  Variables  and  Data  Types  


¡  A  variable  in  PHP  is  a  name  of  memory  
location  that  holds  data.    

¡  A  variable  is  a  temporary  storage  that  


is  used  to  store  data  temporarily.  

¡  In  PHP,  a  variable  is  declared  using  $  


sign  followed  by  variable  name.  

¡  Syntax  of  declaring  a  variable  in  PHP  


is  given  below:  
$variablename=value;        

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


27  

¡  In  PHP,  a  variable  does  not  need  to  be  


declared  before  adding  a  value  to  it.  

¡  the  variable  is  declared  automatically  when  


you  use  it.  

¡  PHP  automatically  converts  the  variable  to  the  


correct  data  type,  depending  on  its  value.  

¡  The  PHP  var_dump()  function  returns  the  data  


type  and  value:Example          
<?php    
     $x  =  5985;  
     var_dump($x);      
       ?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


28  

Naming  Rules  for  Variables  


¡  A  variable  name  must  start  with  a  letter  or  an  
underscore  "_"    

¡  A   variable   name   can   only   contain   alpha-­‐numeric  


characters  and  underscores  (a-­‐z,  A-­‐Z,  and  _  )    

¡  A  variable  name  should  not  contain  spaces.    

¡  If   a   variable   name   is   more   than   one   word,   it  


should   be   separated   with   an   underscore  
($my_string)or  with  capitalization  ($myString).  

 
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
29  

Example  :  
<?php      

     $str="hello  string";     OutPut:  


String  is  :  hello  string  
     $x=100;     Integer  is:100  
Float  is:  22.5  
     $y=22.5;        
 
       echo  ”String  is:  $str<br/>";    
       echo  ”Integer  is:  $x<br/>";    
       echo  ”Float  is:  $y<br/>";      

?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


30  

PHP  Boolean    
¡ A  Boolean  represents  two  possible  
states:  TRUE  or  FALSE.  
$x  =  true;  
$y  =  false;  
¡ Booleans  are  often  used  in  
conditional  testing.    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


31  

PHP  Array  
¡  An  array  stores  multiple  values  in  one  
single  variable.  
¡  In  the  following  example  $cars  is  an  
array.    
¡  The  position  of  array  elements  starts  
from  0  not  1  
Example  
<?php    
               $cars  =  array("Volvo","BMW","Toyota");  
               var_dump($cars);  
               echo  $cars[0];  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


32  

PHP  NULL  Value  


¡  Null  is  a  special  data  type  which  can  have  
only  one  value:  NULL.  
¡  A  variable  of  data  type  NULL  is  a  variable  
that  has  no  value  assigned  to  it.  
           Tip:  If  a  variable  is  created  without  a  
value,  it  is  automatically  assigned  a  value  of  
NULL.  
¡  Variables  can  also  be  emptied  by  setting  the  
value  to  NULL:  
<?php  
     $x  =  "Hello  world!";  
     $x  =  null;  
     var_dump($x);  
?>  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
33  

 <?php  
$x  =  "Hello  world!";  
$x  =  null;  
var_dump($x);  
?>    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


34  

PHP  Object  
 
¡  An  object  is  a  data  type  which  stores  
data  and  information  on  how  to  process  
that  data.  

¡  In  PHP,  an  object  must  be  explicitly  


declared.  

¡  First  we  must  declare  a  class  of  object.    

¡  For  this,  we  use  the  class  keyword.    

¡  A  class  is  a  structure  that  can  contain  


properties  and  methods  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
35  

Cont’d    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


36  

Cont’d    
<?php        
     class  Myclass  
       {  
           public  $font_size  =10;  
       }  
   $f  =  new  MyClass;  
   echo  $f-­‐>font_size;  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


37  
<?php  
 class  Car  {  
       function  Car()    
       {  
               $this-­‐>model  =  “BMW”;  
       }  
     }  
     //  create  an  object  
         $h=  new  Car();  
//  show  object  properties  
       echo  $h-­‐>model;  
?>    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


38  

Manipulate  Strings  
 

¡ A  PHP  string  is  a  sequence  of  


characters  i.e.  used  to  store  and  
manipulate  text.    
   

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


39  

Single  Quoted  PHP  String    


¡ enclosing  text  in  a  single  quote    
<?php      
   $str='Hello  world  within  single  quote';      
 echo  $str;      
?>      
 
Output:  Hello  world  within  single  quote    
 

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


Multiple  line  text,  special   40  
characters  and  escape  sequences  in  a  
single  quoted  String    

<?php      
$str1='Hello  text        
multiple  line      
text  within  single  quoted  string';      
$str2='Using  double  "quote"  directly  inside  single  quoted  string';      
$str3='Using  escape  sequences  \n  in  single  quoted  string';      
echo  "$str1  <br/>  $str2  <br/>  $str3";    
 ?>    

¡   Output:    
         Hello  text  multiple  line  text  within  single  quoted  string    
         Using  double  "quote"  directly  inside  single  quoted  string    
         Using  escape  sequences  \n  in  single  quoted  string    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


Note:   In   single   quoted   PHP   strings,   most   41  
escape     sequences   and   variables   will   not   be  
interpreted.   But,   we   can   use   single   quote  
through   \'   and   backslash   through   \\   inside  
single  quoted  PHP  strings  
<?php      

$num1=10;  

$str1='trying  variable  $num1';    

$str2='trying  backslash  n  and  backslash  t  inside  single  quoted  string  \n  \t';      

$str3='Using  single  quote  \'my  quote\'  and  \\backslash';      

echo  "$str1  <br/>  $str2  <br/>  $str3";    

 ?>      

Output:    

         trying  variable  $num1    


         trying  backslash  n  and  backslash  t  inside  single  quoted  string  \n  \t    
         Using  single  quote  'my  quote'  and  \backslash    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


42  

Double  Quoted  PHP  String  


¡  enclosing  text  within  double  quote  also.    

¡  But  escape  sequences  and  variables  will  


be  interpreted  using  double  quote  PHP  
strings.  
<?php      
     $str="Hello  text  within  double  quote";      
   echo  $str;      
?>      

¡  Output:  Hello  text  within  double  quote    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


43  

Cont’d    
¡   You  can't  use  double  quote  directly  inside  double  
quoted  string.  
<?php      
$str1="Using  double  "quote"  directly  inside  double  quoted  
string";      
echo  $str1;      
?>      

¡  Output:  error    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


44  

Cont’d    
¡   We  can  store  multiple  line  text,  special  characters  and  escape  
sequences  in  a  double  quoted  PHP  string.  
<?php    
$str1="Hello  text      
 multiple  line    
 text  within  double  quoted  string”;  
$str2="Using  double  \"quote
\"  with  backslash  inside  double  quoted  string";      
$str3="Using  escape  sequences  \n  in  double  quoted  string";      
echo  "$str1  <br/>  $str2  <br/>  $str3";      
?>      

¡  Output:    
Hello  text  multiple  line  text  within  double  quoted  string  
Using  double  "quote"  with  backslash  inside  double  quoted  string  Using  escape  
sequences  in  double  quoted  string  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
 
  45  
 
In  double  quoted  strings,  
variable  will  be  interpreted    
<?php    
 $num1=10;        
 echo  "Number  is:  $num1";      
?>    
¡   Output:  Number  is:  10  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


46  

The  Concatenation  Operator  


 
¡  There  is  only  one  string  operator  in  PHP.  

¡  The  concatenation  operator  dot(.)    is  used  


to  put  two  string  values  together.  

¡  To  concatenate  two  string  variables  


together,  use  the  concatenation  operator  
dot(.):  example:  
<?php  
 $txt1=“I’m  interested!";  
 $txt2=“with  php!";  
 echo  $txt1  .  "  "  .  $txt2;  
?>    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


47  

PHP  String  Functions  


1)  strtolower()    
¡  returns  string  in  lowercase  letter.  
¡  Syntax    
 strtolower  (  string  $string  )      

¡  Example  
<?php      
     $str=”Hello  STUDENT";      
     $str=strtolower($str);      
     echo  $str;      
?>      

¡  Output:  hello  student      

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


48  

PHP  String  Functions  


2)  strtoupper()  
¡  returns  string  in  uppercase  letter.  

¡  Syntax  
 strtoupper  (  string  $string  )  

¡  Example  
<?php      
 $str="Hello  STUDENT";      
$str=strtoupper($str);      
echo  $str;            
?>      

¡  Output:  HELLO  STUDENT    


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
49  

PHP  String  Functions  


3)  ucfirst()  
¡  returns  string  converting  first  character  into  
uppercase.  
¡  It  doesn't  change  the  case  of  other  characters  

¡  Syntax  
 ucfirst  (  string  $str  )    
Output  :  Hello  STUDENT  
¡  Example  
<?php      
 $str=“hello  STUDENT";      
$str=ucfirst($str);      
echo  $str;    ?>      
 
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
 
50  

PHP  String  Functions  


4)  lcfirst()    
¡  returns  string  converting  first  character  into  
lowercase.  
¡  It  doesn't  change  the  case  of  other  

¡  Syntax  
   lcfirst  (  string  $str  )  
Output:  hello  Student    
¡  Example        
<?php      
 $str=“Hello  Student";      
$str=lcfirst  ($str);      
echo  $str;    ?>      
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
51  

PHP  String  Functions  


5)  ucwords()    
¡   returns  string  converting  first  character  of  
each  word  into  uppercase.  

¡  Syntax  
¡  string  ucwords  (  string  $str  )    

¡   Example  
Output:  Hello  Student    
<?php      
$str=”hello  student";      
$str=ucwords($str);      
echo  $str;    
 ?>      
   
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
52  

PHP  String  Functions  


6)  strrev()          

¡  returns  reversed  string.  

¡  Syntax  
strrev  (  string  $string  )      

¡  Example  
<?php       Output:  tneduts  olleh    
 
$str=”hello  student";      
$str=strrev($str);      
echo  $str;      
?>      
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
53  

PHP  String  Functions  


7)  strlen()          
¡  returns  length  of  the  string.  

¡  Syntax  :  
                             strlen(  string  $string  )      

¡  Example  :  
 <?php       Output:  13    
$str=”hello  student";        
$str=strlen($str);      
echo  $str;      
?>      

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


54  

PHP  String  Functions  


8.strpos()  :  
¡  The  PHP  strpos()  function  searches  for  a  
specific  text  within  a  string.  
¡  If  a  match  is  found,  the  function  returns  the  
character  position  of  the  first  match.    
¡  If  no  match  is  found,  it  will  return  NULL.  

Tip:  The  first  character  position  in  a  string  


is  0  (not  1).  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


55  

PHP  String  Functions  


¡  The  example  below  searches  for  the  text  
"world"  in  the  string  "Hello  world!":  
 
<?php  
   echo  strpos("Hello  world!",  "world");    
?  >          

¡  output  :  6  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


56  

PHP  String  Functions  


 
9.str_replace()  :  
¡  The  PHP  str_replace()  function  replaces  some  
characters  with  some  other  characters  in  a  
string.  

¡  The  example  below  replaces  the  text  "world"  


with  ”dave":  

Example  
<?php  
echo  str_replace("world”,”Dave”,"Hello  world!");    
?>  
Output:  Hello  Dave!  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


57  

PHP  Math  Functions    


¡  sin()  
¡  abs()   ¡  base_convert()   ¡  getrandmax()   ¡  mt_getrandmax()  
¡  sinh()  
¡  acos()   ¡  bindec()   ¡  hexdec()   ¡  mt_rand()  
¡  sqrt()  
¡  acosh()   ¡  ceil()   ¡  hypot()   ¡  mt_srand()  
¡  srand()  
¡  asin()   ¡  cos()   ¡  is_finite()   ¡  octdec()  
¡  tan()  
¡  asinh()   ¡  cosh()   ¡  is_infinite()  ¡  pi()pow()  
¡  tanh()  
¡  atan()   ¡  decbin()   ¡  is_nan()   ¡  rad2deg()  
¡  max()  
¡  atan2()   ¡  dechex()   ¡  lcg_value()   ¡  rand()  
¡  min()  
¡  atanh()   ¡  decoct()     ¡  log()log10()   ¡  round()  
¡  Fmod()  
¡  exp()     ¡  expm1()   ¡  log1p()   ¡  deg2rad()    
¡  floor()  
   
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
58  

Constants    
¡  PHP  constants  are  name  or  identifier  that  can't  be  
changed  during  the  execution  of  the  script.    

¡  PHP  constants  can  be  defined  by  2  ways:  


¡  Using  define()  function  
¡  Using  const  keyword  
¡  PHP  constants  follow  the  same  PHP  variable  rules.  

¡  No  $  sign  before  the  constant  name.    

¡  Conventionally,  PHP  constants  should  be  defined  in  


uppercase  letters.  

¡  Note:Unlike  variables,  constants  are  automatically  


global  across  the  entire  script.  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
59  

Syntax  
define(name,  value,  case-­‐insensitive)  
Parameters:  
¡  Name:  Specifies  the  name  of  the  constant  
¡  Value:  Specifies  the  value  of  the  
constant  
¡  Case-­‐insensitive:  Specifies  whether  the  
constant  name  should  be  case-­‐
insensitive.  Default  is  false  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


60  

Example:  
¡ The  example  below  creates  a  constant  
with  a  case-­‐sensitive  name:  
 
<?php  
define("GREETING",  "Welcome  to  W3Schools.com!");  
echo  GREETING;  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


61  

PHP  constant:  const  keyword  


 

¡ The  const  keyword  defines  constants  


at  compile  time.    
¡ It  is  a  language  construct  not  a  
function.  
¡ It  is  bit  faster  than  define().  
¡ It  is  always  case  sensitive.  
     
 
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
62  

Example  
 
<?php      
const  MESSAGE="Hello  const  by  PHP";      
echo  MESSAGE;      
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


63  

Array  

¡  An  array  is  a  special  type  of  variable  that  


can  hold  many  values  at  once,  all  
accessible  via  a  single  variable  name.  

   

¡  Arrays  are  very  useful  whenever  you  need  to  


work  with  large  amounts  of  data  —  such  as  
records  from  a  database  —  or  group  related  
data  together.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


64  

Types  of  Arrays  in  PHP  


 
¡ There  are  three  types  of  arrays  that  
you  can  create.  These  are:  
¡ Indexed  array      
¡ An  array  with  a  numeric  key.  
¡ Associative  array      
¡ An  array  where  each  key  has  
its  own  specific  value.  
¡ Multidimensional  array  
¡ An  array  containing  one  or  
more  arrays  within  itself.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


65  

Indexed  Arrays  
 
¡ An  indexed  or  numeric  array  stores  
each  array  element  with  a  numeric  
index.    
¡ Note:    
¡  In  an  indexed  or  numeric  array,  the  
indexes  are  automatically  assigned  and  
start  with  0,  and  the  values  can  be  
any  data  type.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


66  

Indexed  array  
<?php    

//  Define  an  indexed  array    

$colors  =  array("Red”,"Green”,"Blue");    
 $colors[0]  =  "Red";  
 $colors[1]  =  "Green";    
 $colors[2]  =  "Blue";    
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


67  

Associative  Arrays  
 
¡  In  an  associative  array,  the  keys  assigned  
to  values  can  be  arbitrary  and  user  defined  
strings.  
<?php  

 //  Define  an  associative  array    


$ages  =  array("Peter"=>22,"Clark"=>32,"John"=>28);      
$ages["Peter"]  =  "22";    
$ages["Clark"]  =  "32";    
$ages["John"]  =  "28";  
 ?>    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


68  

Multidimensional  Arrays  
 

¡ is  an  array  in  which  each  element  


can  also  be  an  array  and  each  
element  in  the  sub-­‐array  can  be  an  
array  or  further  contain  array  
within  itself  and  so  on.    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


Example:   69  

 <?php  
   $marks  =  array(      
   ”mrx"  =>  array  (  "php"  =>  35,"maths"  =>  30,  ”java"  =>  39  ),  

 "abebe"  =>  array  ("php"  =>  30,"maths"  =>  32,    ”java"  =>  29  ),  

”mry"  =>  array  (  "php"  =>  31,"maths"  =>  22,  ”java"  =>  39  )  );                  

 //  Accessing  multi-­‐dimensional  array  values    


         echo  "Marks  for  mrx  in  physics  :  "  ;    
             echo  $marks[’mrx']['php’]."<br  />";    
         echo  "Marks  for  abebe  in  maths  :  ”;  
             echo  $marks['abebe']['maths’]."<br  />";    
         echo  "Marks  for  mry  in  chemistry  :  "  ;  
               echo  $marks[’mry'][’java']  .  "<br  />";    

           ?>  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
70  
Viewing  Array  Structure  and  
Values  
 
<?php    
//  Define  array    
     $cities  =  array(“Addis",  “DB",  “Markos");    
 //  Display  the  cities  array    
     var_dump($cities);    
?>  
 

<?php  
 //  Define  array    
           $cities  =  array(“Addis",  “DB",  “Markos");    
//  Display  the  cities  array    
             print_r($cities);    
?>  
 
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
71  

PHP  Functions  For  Sorting  Arrays  


¡ sort()  and  rsort()    
¡  For  sorting  indexed  arrays  

¡ asort()  and  arsort()    


¡  For  sorting  associative  arrays  by  value  

¡ ksort()  and  krsort()    


¡  For  sorting  associative  arrays  by  key  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


72  

Sorting  Indexed  Arrays    


 
<?php    
     //  Define  array  
   $colors  =  array("Red",  "Green",  "Blue”,"Yellow");    
 //  Sorting  and  printing  array  in  Ascending  Order    
       sort($colors);    
     print_r($colors);    
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


73  

Sorting  Indexed  Arrays      


 
<?php    
       //  Define  array    
$colors  =  array("Red",  "Green",  "Blue",  "Yellow");    
       //  Sorting  and  printing  arrayin  Descending  Order  
rsort($colors);    
print_r($colors);    
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


74  

Sorting  Associative  Arrays    


 
<?php    
                   //  Define  array    

$age  =  array("Peter"=>20,  "Harry"=>14,  "John"=>45,  "Clark"=>35);    


   //  Sorting  array  by  value  and  print  in  Ascending  Order  By  Value      
asort($age);    
print_r($age);    
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


75  

Sorting  Associative  Arrays    


 
<?php  
           //  Define  array  
$age  =  array("Peter"=>20,  "Harry"=>14,"John"=>45,  "Clark"=>35);  
 //  Sorting  array  by  value  &print  in  Descending  Order  By  Value  
arsort($age);  
print_r($age);  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


76  

Sorting  Associative  Arrays    


 
<?php    

   //  Define  array    

$age  =  array("Peter"=>20,  "Harry"=>14,  "John"=>45,  "Clark"=>35);    

 //  Sorting  array  by  key  and  print  in  Ascending  Order  By  Key    

ksort($age);    

print_r($age);    

?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


77  

Operators  and  expressions    


¡ Operators  :    
¡ they  are  symbols  use  to  manipulate  
values.  
¡ Operand  :    
¡ is  a  value  operate  by  the  operator/s.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


78  

Operators    
PHP  Operators  can  be  categorized  in  following  
forms:  
¡  Arithmetic  Operators  

¡  Comparison  Operators  

¡  Bitwise  Operators  

¡  Logical  Operators  

¡  String  Operators  

¡  Assignment  Operators  

¡  Incrementing/Decrementing  Operators  

¡  Array  Operators  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


79  

¡ We  can  also  categorize  operators  on  


behalf  of  operands.    
¡ They  can  be  categorized  in  3  forms:  
¡ Unary  Operators:  works  on  single  
operands  such  as  ++,  -­‐-­‐  etc.  
¡ Binary  Operators:  works  on  two  
operands  such  as  binary  +,  -­‐,  *,  /  
etc.  
¡ Ternary  Operators:  works  on  three  
operands  such  as  "?:".  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
80  

Arithmetic  Operators  :Example    


<?php  
$x  =  10;      $y  =  6;  
echo($x+$y)."<br>";  //output  16      
echo($x-­‐$y)."<br>";  //output  4  
echo($x*$y)."<br>";  //output  60  
echo($x/$y)."<br>";  //output  1.67  
echo($x%$y)."<br>";  //output  4  
echo  ++$x."<br>";      //  output  11  
echo  -­‐-­‐$y."<br>";      //  output  5  
echo  $x++."<br>";      //  output  11  
echo  $y-­‐-­‐."<br>";      //  output  5  
?>  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
81  

Control  statements    
¡  Conditional  statements    
¡  If    
¡  If..else    
¡  If..elseif..else    
¡  Switch    

¡  Loops    
¡  While    
¡  Do..while    
¡  For    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


82  

Conditional  Statements  
¡  Conditional  statements  are  used  to  perform  
different  actions  based  on  different  conditions.  

¡  if  statement  -­‐  use  this  statement  to  execute  some  


code  only  if  a  specified  condition  is  true    

¡  if...else  statement  -­‐  use  this  statement  to  execute  


some  code  if  a  condition  is  true  and  another  code  
if  the  condition  is  false    

¡  if...elseif....else  statement  -­‐  use  this  statement  


to  select  one  of  several  blocks  of  code  to  be  
executed    

¡  switch  statement  -­‐  use  this  statement  to  select  one  
of  many  blocks  of  code  to  be  executed    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


83  

The  if  Statement  


 
¡  Use  the  if  statement  to  execute  some  code  
only  if  a  specified  condition  is  true.  

Syntax  

¡  if  (condition)  code  to  be  executed  if  


condition  is  true;    

¡  The  following  example  will  output  "Have  a  


nice  weekend!"  if  the  current  day  is  Friday:  

¡  <?php  
$d=date("D");  
if  ($d=="Fri")  echo  "Have  a  nice  weekend!";  
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


84  

if...else  :Syntax  
 
¡  The  if...else  Statement  

¡  Use  the  if....else  statement  to  execute  some  


code  if  a  condition  is  true  and  another  code  
if  a  condition  is  false.  
if  (condition)  
{  
   code  to  be  executed  if  condition  is  true;  
}  
else  
{  
   code  to  be  executed  if  condition  is  false;    
}  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


85  

Example  :if  ..else  


¡  The  following  example  will  output  "Have  a  
nice  weekend!"  if  the  current  day  is  Friday,  
otherwise  it  will  output  "Have  a  nice  day!":  

¡  <?php  
$d=date("D");  
if  ($d=="Fri")  
   {  
   echo  "Hello!<br  />";  
   echo  "Have  a  nice  weekend!";  
   echo  "See  you  on  Monday!";  
   }  
?>  
 

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


86  
The  if...elseif....else  
Statement  
 
¡  Use  the  if....elseif...else  statement  to  
select  one  of  several  blocks  of  code  to  be  
executed.  

¡  Syntax  

   if  (condition)  
   code  to  be  executed  if  condition  is  true;  
elseif  (condition)  
   code  to  be  executed  if  condition  is  true;  
else  
   code  to  be  executed  if  condition  is  
false;  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


87  

Example:  if..elseif..else    
¡  The  following  example  will  output  "Have  a  
nice  weekend!"  if  the  current  day  is  Friday,  
and  "Have  a  nice  Sunday!"  if  the  current  day  
is  Sunday.  Otherwise  it  will  output  "Have  a  
nice  day!":  
¡  <?php  
$d=date("D");  
if  ($d=="Fri")  
   echo  "Have  a  nice  weekend!";  
elseif  ($d=="Sun")  
   echo  "Have  a  nice  Sunday!";  
else  
   echo  "Have  a  nice  day!";  
?>  
 

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


88  

PHP  Switch  Statement  


 
¡  Use  the  switch  statement  to  select  one  of  many  blocks  
of  code  to  be  executed.  

¡  Syntax  
switch  (n)  
{  
case  label1:  
   code  to  be  executed  if  n=label1;  
   break;  
case  label2:  
   code  to  be  executed  if  n=label2;  
       ……………….  
   break;  
default:  
   code  to  be  executed  if  n  is  different  
from  both  label1  and  label2;  
}  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


89  

Switch  Statement    con’t.  


¡  This  is  how  it  works:  First  we  have  a  single  
expression  n  (most  often  a  variable),  that  is  
evaluated  once.    
¡  The  value  of  the  expression  is  then  compared  
with  the  values  for  each  case  in  the  
structure.    
¡  If  there  is  a  match,  the  block  of  code  
associated  with  that  case  is  executed.    
¡  Use  break  to  prevent  the  code  from  running  
into  the  next  case  automatically.    
¡  The  default  statement  is  used  if  no  match  is  
found.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


90  

Example:  Switch  Statement  


 
<?php  
switch  ($x)  
{  
case  1:  
   echo  "Number  1";  
   break;  
case  2:  
   echo  "Number  2";  
   break;  
case  3:  
   echo  "Number  3";  
   break;  
default:  
   echo  "No  number  between  1  and  3";  
}  
?>  
 

 
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
91  

PHP  Looping  -­‐  While  Loops  


¡  Loops  execute  a  block  of  code  a  specified  number  of  
times,  or  while  a  specified  condition  is  true.  

¡  In  PHP,  we  have  the  following  looping  statements:  

¡  while  -­‐  loops  through  a  block  of  code  while  a  


specified  condition  is  true    

¡  do...while  -­‐  loops  through  a  block  of  code  once,  


and  then  repeats  the  loop  as  long  as  a  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    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


92  

The  while  Loop  


 
¡  The  while  loop  executes  a  block  of  code  while  
a  condition  is  true.  
¡  Syntax  
while  (condition)  
   {  
   code  to  be  executed;  
   }    
¡  Example  
¡  The  example  below  defines  a  loop  that  starts  
with  i=1.  The  loop  will  continue  to  run  as  
long  as  i  is  less  than,  or  equal  to  5.  i  will  
increase  by  1  each  time  the  loop  runs:  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


93  

Example:  while  loop    


¡  <?php  
$i=1;  
while($i<=5)  
   {  
   echo  "The  number  is  "  .  $i  .  "<br  />";  
   $i++;  
   }  
?>  
Output:  
¡  The  number  is  1  
The  number  is  2  
The  number  is  3  
The  number  is  4  
The  number  is  5  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


94  

The  do...while  Statement  


 
¡  The  do...while  statement  will  always  execute  the  block  of  code  
once,  it  will  then  check  the  condition,  and  repeat  the  loop  
while  the  condition  is  true.  

¡  Syntax  

do  
   {  
   code  to  be  executed;  
   }  
while  (condition);      

¡  Example  

¡  The  example  below  defines  a  loop  that  starts  with  i=1.  It  will  
then  increment  i  with  1,  and  write  some  output.  Then  the  
condition  is  checked,  and  the  loop  will  continue  to  run  as  
long  as  i  is  less  than,  or  equal  to  5:  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


95  

Example:  do…while  
¡  <?php  
$i=1;  
do  
   {  
   $i++;  
   echo  "The  number  is  "  .  $i  .  "<br  />";  
   }  
while  ($i<=5);  
?>  
Output:  
¡  The  number  is  2  
The  number  is  3  
The  number  is  4  
The  number  is  5  
The  number  is  6  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


96  

PHP  Looping  -­‐  For  Loops  


¡  The  for  loop  is  used  when  you  know  in  advance  how  
many  times  the  script  should  run.  Syntax  :  
for  (init;  condition;  increment)  
   {  
   code  to  be  executed;  
   }      

¡  Parameters:  
¡  init:  Mostly  used  to  set  a  counter  (but  can  be  any  code  to  be  executed  
once  at  the  beginning  of  the  loop)    
¡  condition:  Evaluated  for  each  loop  iteration.If  it  evaluates  to  TRUE,  
the  loop  continues.  If  it  evaluates  to  FALSE,  the  loop  ends.    
¡  increment:  Mostly  used  to  increment  a  counter  (but  can  be  any  code  to  
be  executed  at  the  end  of  the  loop)    

¡  Note:  Each  of  the  parameters  above  can  be  empty,  or  have  
multiple  expressions  (separated  by  commas).  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


97  

Example  :for  loop  


¡  The  example  below  defines  a  loop  that  starts  with  
i=1.  The  loop  will  continue  to  run  as  long  as  i  is  
less  than,  or  equal  to  5.  i  will  increase  by  1  each  
time  the  loop  runs:  
¡  <?php  
for  ($i=1;  $i<=5;  $i++)  
   {  
   echo  "The  number  is  "  .  $i  .  "<br  />";  
   }  
?>    

¡  Output:  
   The  number  is  1  
The  number  is  2  
The  number  is  3  
The  number  is  4  
The  number  is  5  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


98  

The  foreach  Loop  


¡  The  foreach  loop  is  used  to  loop  through  arrays.  

Syntax  
foreach  ($array  as  $value)  
   {  
   code  to  be  executed;  
   }    

¡  For  every  loop  iteration,  the  value  of  the  current  array  element  
is  assigned  to  $value  (and  the  array  pointer  is  moved  by  one)  -­‐  
so  on  the  next  loop  iteration,  you'll  be  looking  at  the  next  
array  value.  

Example  

¡  The  following  example  demonstrates  a  loop  that  will  print  the  
values  of  the  given  array:  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


99  

Example:  for  each  


<?php  
 
$data=array("abebe"=>2,"belen"=>1,”feven"=>5);  
foreach($data  as  $name=>$id)  
{  
       echo  $data[$id];  
}  
 
?>  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
0  

Functions      
¡  A  function  is  a  named  block  of  code  that  is  
designed  to  perform  a  specific  task.  

¡   Once  a  function  is  defined,  you  can  reuse  


it  without  copying  and  pasting  a  code  block  
again  and  again.    

¡  A  function  may  accept  one  or  more  


arguments,  which  are  the  values  that  you  
pass  to  the  function.    

¡  A  function  may  return  a  value  so  that  the  


calling  script  can  communicate  with  it.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
1  

Create  a  PHP  Function  


¡  A  function  will  be  executed  by  a  call  to  the  
function.  

¡  Syntax  
function  function_Name()  
{  
code  to  be  executed;  
}  

¡  PHP  function  guidelines:  


¡  Give  the  function  a  name  that  reflects  what  the  
function  does    
¡  The  function  name  can  start  with  a  letter  or  
underscore  (not  a  number)    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
2  
Creating  and  calling  functions  
 
<?php  
 function  function_name(parameter1,  
parameter2,...){  
       //function  body  code  
}  
?>  

function_name(parameter1,  parameter2,...);  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
3  

PHP  Functions  
¡  The  real  power  of  PHP  comes  from  its  
functions.  

¡  In  PHP,  there  are  more  than  700  built-­‐in  


functions.  

¡  when  the  page  loads,  you  can  put  it  into  a  
function.  

¡  A  function  will  be  executed  by  a  call  to  


the  function.  

¡  You  may  call  a  function  from  anywhere  


within  a  page.  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  
10
4  
Example  
A  simple  function  that  writes  name  when  it  is  
called:  
¡  <?php  
function  writeName()  
{  
echo  “mrx";  
}  
 
echo  "My  name  is  ";  
writeName();  
?>  

¡  Output:  
My  name  is  mrx  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
5  
Function  parameters  
 
¡  Information  can  be  passed  to  functions  
through  arguments.  An  argument  is  just  like  a  
variable.  

¡  Function  may  accept  one  or  more  parameters.  


There  are  two  ways  to  pass  parameters  to  a  
function:  passing  parameter  by  value  and  
passing  parameter  by  reference.  

¡  Parameter  is  what's  given  in  the  function  


declaration/definition.  

¡  Argument  is  what's  passed  when  calling  the  


function.  
 

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
6  
 
Passing  parameters  by  
value  
 
Passing  parameters  by  
  reference  
<?php <?php    

function increase($x){ function  increase(&$x){  

$x = $x + 1;  $x  =  $x  +  1;  

echo 'inside function x = ' . $x .'<br />';  echo  'inside  function  x  =  '  .  $x  .'<br  />';  

} }  

$x = 10; $x  =  10;  

increase($x); increase($x);  

echo 'outside function x = ' . $x .'<br />'; echo  'outside  function  x  =  '  .  $x  .'<br  />';  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
7  
Benefits  of  using  function  
 
¡  Functions  are  reusable  –  Because  a  function  
is  designed  to  perform  a  specific  independent  
task  so  that  it  can  be  reused  in  other  web  
applications.  

¡  Functions  help  avoid  duplicate  code  –  A  


function  helps  avoid  copying  and  pasting  code  
all  over  places  by  wrapping  the  logic  and  
assigning  it  a  name.  

¡  Functions  make  your  script  modular  –  by  using  


functions,  a  big  script  is  divided  into  many  
functions  that  are  easier  to  build,  test  and  
maintain.  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  


10
8  

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                   13  March  2020  

You might also like