KEMBAR78
Unit-1 PHP Basic1 of the understanding of php.pptx
Unit-1 PHP Basic
Sohil Parmar
Introduction to PHP
• PHP is an acronym for "PHP: Hypertext
Preprocessor“
• PHP developed by Rasmus Lerdoff in 1994.
• PHP is a widely-used, open source scripting
language
• PHP scripts are executed on the server
• PHP is free to download and use
• PHP files can contain text, HTML, CSS, JavaScript,
and PHP code
Continue…
• PHP code is executed on the server, and the
result is returned to the browser as plain
HTML
• PHP files have extension ".php"
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and
close files on the server
• PHP runs on various platforms (Windows,
Linux, Unix, Mac OS X, etc.)
Continue…
• PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
• PHP supports a wide range of databases
• PHP is free. Download it from the official PHP
resource: www.php.net
• PHP is easy to learn and runs efficiently on the
server side.
PHP configuration in IIS & Apache Web
server
• On the Start page, click the Server
Manager tile, and then click OK.
• In Server Manager, select Dashboard, and
click Add roles and features.
• In the Add Roles and Features Wizard, on
the Before You Begin page, click Next.
• On the Select Installation Type page,
select Role-based or Feature-based
Installation and click Next
Continue…
• On the Select Destination Server page,
select Select a server from the server pool, select
your server, and click Next.
• On the Select Server Roles page, select Web
Server (IIS), and then click Next.
• On the Select Features page, note the
preselected features that are installed by default,
and then select CGI. This selection also installs
FastCGI, which is recommended for PHP
applications.
• Click Next.
Continue…
• On the Web Server Role (IIS) page, click Next.
• On the Select Role Services page, note the
preselected role services that are installed by
default, and then click Next.
• On the Confirm Installation Selections page,
confirm your selections, and then click Install.
• On the Installation Progress page, confirm that
your installation of the Web Server (IIS) role and
required role services completed successfully,
and then click Close.
Continue…
• To verify that IIS installed successfully, type
the following into a web browser:
http://localhost
• You should see the default IIS Welcome page.
Understanding of PHP.INI file
• At the time of PHP installation, php.ini was a
special file provided as a default configuration
file.
• It’s a very essential configuration file that controls
what a user can or cannot do with the website.
• Each time PHP is initialized, the php.ini file is read
by the system.
• Sometimes you need to change the behaviour of
PHP at runtime, then this configuration file is to
use.
Continue…
• php.ini file is the configuration file. It is always
checked when the server gets started or HTTP is
restarted in the module and it configures the
website to know what a user can do or can’t do
with a website.
• Whenever some changes are made to the file,
you need to restart your web server.
• To check the file path, use the following program:
• echo phpinfo();
Understanding of PHP .htaccess file
• .htaccess is a configuration file for use on web
servers running on the web apache server
software.
• when a .htaccess file is placed in a directory
which in turn loaded via the Apache web server,
then the .htaccess file detected and executed by
the Apache server software.
• .htaccess files can be utilized to modify the setup
of the Apache server software to empower
additional functionality and fetures that the
apache web server softwatre brings to the table.
Continue…
• .htaccess files can be utilized to modify the setup
of the Apache server software to empower
additional functionality and fetures thath te
apache web server softwatre brings to the table.
• ErrorDocuments
• ErrorDocument 404 /error_pages/404.html
• Redirection
• Redirect /old_dir/ http://www.test.com(your
domain)/new_dir/index.html
Continue…
• Deny visitors by IP address
• order allow,deny
deny from 155.0.2.0
deny from 123.45.6.1
allow from all
PHP Variable
• In PHP, a variable starts with the $ sign,
followed by the name of the variable:
• A variable can have a short name (like x and y)
or a more descriptive name (age, carname,
total_volume).
Rules for PHP variables:
• A variable starts with the $ sign, followed by the
name of the variable
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive
($age and $AGE are two different variables)
PHP Variables Scope
• Local.
• Global
• Static
PHP Operator
• PHP divides the operators in the following
groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
PHP Arithmetic Operators
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th
power
PHP Assignment Operators
Operator Same as... Descriptione
x = y x = y The left operand gets set to the value of the
expression on the right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus
PHP Comparison Operators
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they
are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or
they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than
or equal to
$x >= $y Returns true if $x is greater than or equal
to $y
<= Less than or
equal to
$x <= $y Returns true if $x is less than or equal to
$y
PHP Increment / Decrement Operators
Operator Name Description
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one
PHP Logical Operators
Operator Name Example Result
and And $x and $y True if both $x and $y are
true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true,
but not both
&& And $x && $y True if both $x and $y are
true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
PHP String Operators
Operator Name Example Result
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and
$txt2
.= Concatenation
assignment
$txt1 .= $txt2 Appends $txt2 to $txt1
PHP Array Operators
Operator Name Example Result
+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the
same key/value pairs
=== Identity $x === $y Returns true if $x and $y have the
same key/value pairs in the same
order and of the same types
!= Inequality $x != $y Returns true if $x is not equal to
$y
<> Inequality $x <> $y Returns true if $x is not equal to
$y
!== Non-identity $x !== $y Returns true if $x is not identical
to $y
Conditional Structure
• 1. The if Statement
• 2. The if...else Statement
• 3. The if...elseif...else Statement
• 4. The switch Statement
PHP - The if Statement
• The if statement executes some code if one
condition is true.
• Syntax
if (condition) {
code to be executed if condition is true;
}
Continue…
• Example:
<?php
$x=10;
if($x < 5)
{
echo “True”;
}
?>
The if...else Statement
• The if...else statement executes some code if a
condition is true and another code if that
condition is false.
• Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Continue…
• Example
<?php
$x=10;
if($x < 5)
{
echo “True”;
}else{
echo “x is not less then 5”;
}
?>
The if...elseif...else Statement
• The if...elseif...else statement executes different
codes for more than two conditions.
• Syntax:
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this
condition is true;
} else {
code to be executed if all conditions are false;
}
Continue…
• Example
<?php
$x=0;
if($x > 0)
{
echo “x is greater than zero”;
}elseif($x<0){
echo “x is less than zero”;
}else{
echo “x is zero”;
}
?>
The switch Statement
• Use the switch statement to select one of
many blocks of code to be executed.
• Suntax:
Continue…
• Example:
PHP Loops
• Loops are used to execute the same block of
code again and again, as long as a certain
condition is true.
• 1. while - loops through a block of code as
long as the specified condition is true
• 2. do...while - loops through a block of code
once, and then repeats the loop as long as the
specified condition is true
Continue…
• 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
PHP while Loop
• The while loop executes a block of code as
long as the specified condition is true.
• Syntax
while (condition is true) {
code to be executed;
}
Continue…
• Examples
• The example below displays the numbers from 1
to 10:
<?php
$x = 1;
while($x <= 10) {
echo "The number is: $x <br>";
$x++;
}
?>
do while Loop
• The do...while loop will always execute the
block of code once, it will then check the
condition, and repeat the loop while the
specified condition is true.
• Syntax:
do {
code to be executed;
} while (condition is true);
Continue…
• Example:
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 10);
?>
PHP for Loop
• The for loop is used when you know in
advance how many times the script should
run.
• Syntax:
for (init counter; test counter; increment
counter) {
code to be executed for each iteration;
}
Continue…
• Example:
<?php
for ($x = 1; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
foreach Loop
• The foreach loop works only on arrays, and is
used to loop through each key/value pair in an
array.
• Syntax:
foreach ($array as $value) {
code to be executed;
}
Continue…
• Example:
<?php
$colors
= array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
Array
• An array is a special variable, which can hold
more than one value at a time.
• An array stores multiple values in one single
variable:
• In PHP, there are three types of arrays:
1. Indexed arrays - Arrays with a numeric index
2. Associative arrays - Arrays with named keys
3. Multidimensional arrays - Arrays containing
one or more arrays
Indexed Arrays
• The index can be assigned automatically
(index always starts at 0), like this:
• $cars = array("Volvo", "BMW", "Toyota");
• Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
print_r($cars);
?>
Associative Arrays
• Associative arrays are arrays that use named
keys that you assign to them.
• $age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
Continue…
• To loop through and print all the values of an
associative array, you could use
a foreach loop, like this:
Multidimensional Arrays
• A multidimensional array is an array
containing one or more arrays.
• PHP supports multidimensional arrays that are
two, three, four, five, or more levels deep.
However, arrays more than three levels deep
are hard to manage for most people.
User Defined Functions:
• Besides the built-in PHP functions, it is
possible to create your own functions.
• A function is a block of statements that can be
used repeatedly in a program.
• A function will not execute automatically
when a page loads.
• A function will be executed by a call to the
function.
Continue…
• Syntax:
function functionName() {
code to be executed;
}
Example:
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
argument function
• Information can be passed to functions
through arguments. An argument is just like a
variable.
• Arguments are specified after the function
name, inside the parentheses. You can add as
many arguments as you want, just separate
them with a comma.
Continue…
• Example:
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
?>
default argument
• example shows how to use a default parameter. If we
call the function setHeight() without arguments it takes
the default value as argument:
• Example:
<?php
declare(strict_types=1); // strict requirement
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
?>
Variable function
• If name of a variable has parentheses (with or without
parameters in it) in front of it, PHP parser tries to find a
function whose name corresponds to value of the
variable and executes it.
<?php
function hello(){
echo "Hello World";
}
$var="Hello";
$var();
?>
Return function
• Example :
<?php
function add1($x) {
return $x + 1;
}
echo "5 + 1 is " . add1(5);
?>
Variable Length Argument Function
• func_num_args :
• The func_num_args() function can return the
number of arguments passed into current
user-defined function.
• Example:
func_get_arg
• The func_get_arg() function is an inbuilt
function in PHP which is used to get a
mentioned value from the argument passed
as the parameters.
func_get_args
• The func_get_args() function can return an
array comprising a function's argument list.
Built in Functions
• Variable Functions
• If name of a variable has parentheses (with or
without parameters in it) in front of it, PHP
parser tries to find a function whose name
corresponds to value of the variable and
executes it.
• Such a function is called variable function.
Continue…
Example:
<?php
function add($x, $y)
{
echo $x+$y;
}
$var="add";
$var(10,20);
?>
String Function
• Strcmp(string1,string2)
• str_replace(find,replace,string)
• strlen(string)
• strrev()
• strtolower(string)
• strtoupper(string)
• trim(string,charlist)
• ucwords(string)
• ucfirst(string)
Math Function
• rand(min,max);
• abs(number);
• bindec(binary_string);
• decbin(number);
• decoct(number);
• pow(x,y);
Date Function
• date(‘Y-m-d’);
• Date(‘d-m-Y’);
• Getdate(); // array varible use print_r
• gettimeofday();
• gmdate(format);
• Localtime();
Array Function
• array_merge(array1, array2, array3, ...);
• array_pop(array);
• array_push(array, value1, value2, ...);
• array_reverse(array);
• array_slice(array, start, length);
• asort(array);
• rsort(array);
• Sort(array);
Miscellaneous Function
• Include();
• Require();
• Die();
• Exit();
• Include_once();
• Require_once();
File handling Function
• readfile()
• fread()
• fclose()
• feof()
• fgetc()
Thankyou…

Unit-1 PHP Basic1 of the understanding of php.pptx

  • 1.
  • 2.
    Introduction to PHP •PHP is an acronym for "PHP: Hypertext Preprocessor“ • PHP developed by Rasmus Lerdoff in 1994. • PHP is a widely-used, open source scripting language • PHP scripts are executed on the server • PHP is free to download and use • PHP files can contain text, HTML, CSS, JavaScript, and PHP code
  • 3.
    Continue… • PHP codeis executed on the server, and the result is returned to the browser as plain HTML • PHP files have extension ".php" • PHP can generate dynamic page content • PHP can create, open, read, write, delete, and close files on the server • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • 4.
    Continue… • PHP iscompatible with almost all servers used today (Apache, IIS, etc.) • PHP supports a wide range of databases • PHP is free. Download it from the official PHP resource: www.php.net • PHP is easy to learn and runs efficiently on the server side.
  • 5.
    PHP configuration inIIS & Apache Web server • On the Start page, click the Server Manager tile, and then click OK. • In Server Manager, select Dashboard, and click Add roles and features. • In the Add Roles and Features Wizard, on the Before You Begin page, click Next. • On the Select Installation Type page, select Role-based or Feature-based Installation and click Next
  • 6.
    Continue… • On theSelect Destination Server page, select Select a server from the server pool, select your server, and click Next. • On the Select Server Roles page, select Web Server (IIS), and then click Next. • On the Select Features page, note the preselected features that are installed by default, and then select CGI. This selection also installs FastCGI, which is recommended for PHP applications. • Click Next.
  • 7.
    Continue… • On theWeb Server Role (IIS) page, click Next. • On the Select Role Services page, note the preselected role services that are installed by default, and then click Next. • On the Confirm Installation Selections page, confirm your selections, and then click Install. • On the Installation Progress page, confirm that your installation of the Web Server (IIS) role and required role services completed successfully, and then click Close.
  • 8.
    Continue… • To verifythat IIS installed successfully, type the following into a web browser: http://localhost • You should see the default IIS Welcome page.
  • 9.
    Understanding of PHP.INIfile • At the time of PHP installation, php.ini was a special file provided as a default configuration file. • It’s a very essential configuration file that controls what a user can or cannot do with the website. • Each time PHP is initialized, the php.ini file is read by the system. • Sometimes you need to change the behaviour of PHP at runtime, then this configuration file is to use.
  • 10.
    Continue… • php.ini fileis the configuration file. It is always checked when the server gets started or HTTP is restarted in the module and it configures the website to know what a user can do or can’t do with a website. • Whenever some changes are made to the file, you need to restart your web server. • To check the file path, use the following program: • echo phpinfo();
  • 11.
    Understanding of PHP.htaccess file • .htaccess is a configuration file for use on web servers running on the web apache server software. • when a .htaccess file is placed in a directory which in turn loaded via the Apache web server, then the .htaccess file detected and executed by the Apache server software. • .htaccess files can be utilized to modify the setup of the Apache server software to empower additional functionality and fetures that the apache web server softwatre brings to the table.
  • 12.
    Continue… • .htaccess filescan be utilized to modify the setup of the Apache server software to empower additional functionality and fetures thath te apache web server softwatre brings to the table. • ErrorDocuments • ErrorDocument 404 /error_pages/404.html • Redirection • Redirect /old_dir/ http://www.test.com(your domain)/new_dir/index.html
  • 13.
    Continue… • Deny visitorsby IP address • order allow,deny deny from 155.0.2.0 deny from 123.45.6.1 allow from all
  • 14.
    PHP Variable • InPHP, a variable starts with the $ sign, followed by the name of the variable: • A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
  • 15.
    Rules for PHPvariables: • A variable starts with the $ sign, followed by the name of the variable • A variable name must start with a letter or the underscore character • A variable name cannot start with a number • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) • Variable names are case-sensitive ($age and $AGE are two different variables)
  • 16.
    PHP Variables Scope •Local. • Global • Static
  • 17.
    PHP Operator • PHPdivides the operators in the following groups: • Arithmetic operators • Assignment operators • Comparison operators • Increment/Decrement operators • Logical operators • String operators • Array operators
  • 18.
    PHP Arithmetic Operators OperatorName Example Result + Addition $x + $y Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y ** Exponentiation $x ** $y Result of raising $x to the $y'th power
  • 19.
    PHP Assignment Operators OperatorSame as... Descriptione x = y x = y The left operand gets set to the value of the expression on the right x += y x = x + y Addition x -= y x = x - y Subtraction x *= y x = x * y Multiplication x /= y x = x / y Division x %= y x = x % y Modulus
  • 20.
    PHP Comparison Operators OperatorName Example Result == Equal $x == $y Returns true if $x is equal to $y === Identical $x === $y Returns true if $x is equal to $y, and they are of the same type != Not equal $x != $y Returns true if $x is not equal to $y <> Not equal $x <> $y Returns true if $x is not equal to $y !== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type > Greater than $x > $y Returns true if $x is greater than $y < Less than $x < $y Returns true if $x is less than $y >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y <= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
  • 21.
    PHP Increment /Decrement Operators Operator Name Description ++$x Pre-increment Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one
  • 22.
    PHP Logical Operators OperatorName Example Result and And $x and $y True if both $x and $y are true or Or $x or $y True if either $x or $y is true xor Xor $x xor $y True if either $x or $y is true, but not both && And $x && $y True if both $x and $y are true || Or $x || $y True if either $x or $y is true ! Not !$x True if $x is not true
  • 23.
    PHP String Operators OperatorName Example Result . Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2 .= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1
  • 24.
    PHP Array Operators OperatorName Example Result + Union $x + $y Union of $x and $y == Equality $x == $y Returns true if $x and $y have the same key/value pairs === Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types != Inequality $x != $y Returns true if $x is not equal to $y <> Inequality $x <> $y Returns true if $x is not equal to $y !== Non-identity $x !== $y Returns true if $x is not identical to $y
  • 25.
    Conditional Structure • 1.The if Statement • 2. The if...else Statement • 3. The if...elseif...else Statement • 4. The switch Statement
  • 26.
    PHP - Theif Statement • The if statement executes some code if one condition is true. • Syntax if (condition) { code to be executed if condition is true; }
  • 27.
  • 28.
    The if...else Statement •The if...else statement executes some code if a condition is true and another code if that condition is false. • Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • 29.
    Continue… • Example <?php $x=10; if($x <5) { echo “True”; }else{ echo “x is not less then 5”; } ?>
  • 30.
    The if...elseif...else Statement •The if...elseif...else statement executes different codes for more than two conditions. • Syntax: if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; }
  • 31.
    Continue… • Example <?php $x=0; if($x >0) { echo “x is greater than zero”; }elseif($x<0){ echo “x is less than zero”; }else{ echo “x is zero”; } ?>
  • 32.
    The switch Statement •Use the switch statement to select one of many blocks of code to be executed. • Suntax:
  • 33.
  • 34.
    PHP Loops • Loopsare used to execute the same block of code again and again, as long as a certain condition is true. • 1. while - loops through a block of code as long as the specified condition is true • 2. do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • 35.
    Continue… • 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
  • 36.
    PHP while Loop •The while loop executes a block of code as long as the specified condition is true. • Syntax while (condition is true) { code to be executed; }
  • 37.
    Continue… • Examples • Theexample below displays the numbers from 1 to 10: <?php $x = 1; while($x <= 10) { echo "The number is: $x <br>"; $x++; } ?>
  • 38.
    do while Loop •The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. • Syntax: do { code to be executed; } while (condition is true);
  • 39.
    Continue… • Example: <?php $x =1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 10); ?>
  • 40.
    PHP for Loop •The for loop is used when you know in advance how many times the script should run. • Syntax: for (init counter; test counter; increment counter) { code to be executed for each iteration; }
  • 41.
    Continue… • Example: <?php for ($x= 1; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?>
  • 42.
    foreach Loop • Theforeach loop works only on arrays, and is used to loop through each key/value pair in an array. • Syntax: foreach ($array as $value) { code to be executed; }
  • 43.
    Continue… • Example: <?php $colors = array("red","green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>
  • 44.
    Array • An arrayis a special variable, which can hold more than one value at a time. • An array stores multiple values in one single variable: • In PHP, there are three types of arrays: 1. Indexed arrays - Arrays with a numeric index 2. Associative arrays - Arrays with named keys 3. Multidimensional arrays - Arrays containing one or more arrays
  • 45.
    Indexed Arrays • Theindex can be assigned automatically (index always starts at 0), like this: • $cars = array("Volvo", "BMW", "Toyota"); • Example: <?php $cars = array("Volvo", "BMW", "Toyota"); print_r($cars); ?>
  • 46.
    Associative Arrays • Associativearrays are arrays that use named keys that you assign to them. • $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
  • 47.
    Continue… • To loopthrough and print all the values of an associative array, you could use a foreach loop, like this:
  • 48.
    Multidimensional Arrays • Amultidimensional array is an array containing one or more arrays. • PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
  • 49.
    User Defined Functions: •Besides the built-in PHP functions, it is possible to create your own functions. • A function is a block of statements that can be used repeatedly in a program. • A function will not execute automatically when a page loads. • A function will be executed by a call to the function.
  • 50.
    Continue… • Syntax: function functionName(){ code to be executed; } Example: <?php function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function ?>
  • 51.
    argument function • Informationcan be passed to functions through arguments. An argument is just like a variable. • Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
  • 52.
    Continue… • Example: <?php function familyName($fname){ echo "$fname Refsnes.<br>"; } familyName("Jani"); ?>
  • 53.
    default argument • exampleshows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument: • Example: <?php declare(strict_types=1); // strict requirement function addNumbers(int $a, int $b) { return $a + $b; } echo addNumbers(5, "5 days"); ?>
  • 54.
    Variable function • Ifname of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. <?php function hello(){ echo "Hello World"; } $var="Hello"; $var(); ?>
  • 55.
    Return function • Example: <?php function add1($x) { return $x + 1; } echo "5 + 1 is " . add1(5); ?>
  • 56.
    Variable Length ArgumentFunction • func_num_args : • The func_num_args() function can return the number of arguments passed into current user-defined function. • Example:
  • 57.
    func_get_arg • The func_get_arg()function is an inbuilt function in PHP which is used to get a mentioned value from the argument passed as the parameters.
  • 58.
    func_get_args • The func_get_args()function can return an array comprising a function's argument list.
  • 59.
    Built in Functions •Variable Functions • If name of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. • Such a function is called variable function.
  • 60.
    Continue… Example: <?php function add($x, $y) { echo$x+$y; } $var="add"; $var(10,20); ?>
  • 61.
    String Function • Strcmp(string1,string2) •str_replace(find,replace,string) • strlen(string) • strrev() • strtolower(string) • strtoupper(string) • trim(string,charlist) • ucwords(string) • ucfirst(string)
  • 62.
    Math Function • rand(min,max); •abs(number); • bindec(binary_string); • decbin(number); • decoct(number); • pow(x,y);
  • 63.
    Date Function • date(‘Y-m-d’); •Date(‘d-m-Y’); • Getdate(); // array varible use print_r • gettimeofday(); • gmdate(format); • Localtime();
  • 64.
    Array Function • array_merge(array1,array2, array3, ...); • array_pop(array); • array_push(array, value1, value2, ...); • array_reverse(array); • array_slice(array, start, length); • asort(array); • rsort(array); • Sort(array);
  • 65.
    Miscellaneous Function • Include(); •Require(); • Die(); • Exit(); • Include_once(); • Require_once();
  • 66.
    File handling Function •readfile() • fread() • fclose() • feof() • fgetc()
  • 67.