SlideShare a Scribd company logo
1 of 53
18IST61 – OPEN SOURCE
SYSTEMS
Unit – I Basics of PHP
Contents
PHP’s Syntax – Comments – Variables – Types in PHP – The Simple Types –
Doubles – Booleans – NULL – Strings – Output – Expressions – Branching –
Looping – Using Functions – User Defined Functions – Functions and Variable
Scope – Function Scope
What is PHP?
● PHP is the web development language written by and for web developers.
● PHP stands for PHP: Hypertext Preprocessor
● The product was originally named Personal Home Page
● PHP is a server-side scripting language, usually used to create web
applications in combination with a web server, such as Apache
● PHP is a widely-used, open source scripting language
● Embedded within PHP
● Develop dynamic web sites
● Easy integration with DB
● Cross-platform compatibility
PHP’s Syntax
● A PHP script can be placed anywhere in the document.
● A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
● The default file extension for PHP files is ".php".
● A PHP file normally contains HTML tags, and some PHP scripting code.
Sample Program
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP is whitespace insensitive
For example, each of the following PHP statements that assigns the sum
of 2 + 2 to the variable
$four is equivalent:
$four = 2 + 2; // single spaces
$four <tab>=<tab>2<tab>+<tab>2 ; // spaces and tabs
$four =
2
+
2; // multiple lines
PHP is sometimes case sensitive
In particular, all variables are case sensitive.
If you embed the following code in an HTML page:
<?php
$capital = 67;
print(“Variable capital is $capital<BR>”);
print(“Variable CaPiTaL is $CaPiTaL<BR>”);
?>
The output you will see is:
Variable capital is 67
Variable CaPiTaL is
Statements are expressions terminated by semicolons
● A statement in PHP is any expression that is followed by a semicolon (;).
● Example:
$greeting = “Welcome to PHP!”;
Comments
A comment is the portion of a program that exists only for the human reader.
● C-style multiline comments
○ The multiline style of commenting is the same as in C: A comment starts with the
character pair /* and terminates with the character pair */.
○ For example: /* This is a comment in PHP */
● Single-line comments: # and //
○ PHP supports two different ways of commenting to the end of a given line: one
inherited from C++ and Java and the other from Perl and shell scripts.
○ The shell-script-style comment starts with a pound sign, whereas the C++ style
comment starts with two forward slashes.
○ Example: # This is a comment
// This is a comment too
Variables
● Variables are "containers" for storing information.
● All variables in PHP are denoted with a leading dollar sign ($).The value of a
variable is the value of its most recent assignment.
● Variables are assigned with the = operator, with the variable on the left-hand side
and the
● expression to be evaluated on the right.
● Variables can, but do not need, to be declared before assignment.
● Variables have no intrinsic type other than the type of their current value.
● Variables used before they are assigned have default values.
● PHP variables are Perl-like
○ All variables in PHP start with a leading $ sign just like scalar variables in the
Perl scripting language
● Declaring variables (or not)
○ In PHP, types are associated with values rather than variables, no such
declaration is necessary
○ The first step in using a variable is to assign it a value.
● Assigning variables
○ Variable assignment is simple — just write the variable name, and add a single
equal sign (=); then add the expression that you want to assign to that
variable: $pi = 3 + 0.14159;.
● Reassigning variables
○ There is no interesting distinction in PHP between assigning a variable for the
first time and changing its value later
○ Example :$my_num_var = “This should be a number – hope it’s reassigned”;
$my_num_var = 5;
● Unassigned variables
○ In a situation where a number is expected, a number will be produced, and
this works similarly with character strings.
○ In any context that treats a variable as a number, an unassigned variable will
be evaluated as 0; in any context that expects a string value, an unassigned
variable will be the empty string
● Variable scope
○ In PHP, variables can be declared anywhere in the script.
○ The scope of a variable is the part of the script where the variable can be
referenced/used.
○ PHP has two different variable scopes:
● Local
A variable declared within a function has a LOCAL SCOPE and can
only be accessed within that function
● Global
A variable declared outside a function has a GLOBAL SCOPE and can
only be accessed outside a function
Types in PHP
● PHP’s type system is simple, streamlined, and flexible, and it insulates the
programmer from low-level details.
● PHP makes it easy not to worry too much about typing of variables and values, both
because it does not require variables to be typed and because it handles a lot of
type conversions
No variable type declarations
● The type of a variable does not need to be declared in advance
Automatic type conversion
● PHP does a good job of automatically converting types when necessary
● $pi = 3 + 0.14159;
● The result of the expression is a floating-point (double) number, with the integer
3 implicitly converted into floating point before the addition is performed.
Type Summary
● Integers are whole numbers, without a decimal point, like 495.
● Doubles are floating-point numbers, like 3.14159 or 49.0.
● Booleans have only two possible values: TRUE and FALSE.
● NULL is a special type that only has one value: NULL.
● Strings are sequences of characters, like ‘PHP 4.0 supports string operations.’
● Arrays are named and indexed collections of other values.
● Objects are instances of programmer-defined classes, which can package up both
other kinds of values and functions that are specific to the class.
● Resources are special variables that hold references to resources external to PHP
(such as database connections).
The Simple Types
● The most of the simple types in PHP (integers, doubles, Booleans, NULL, and
strings) should be familiar to those with programming experience
Integers
● Integers are the simplest type — they correspond to simple whole numbers,
both positive and negative.
● Integers can be assigned to variables, or they can be used in expressions, like
this:
○ $int_var = 12345;
○ $another_int = -12345 + 12345;
The Simple Types
Doubles
● Doubles are floating-point numbers, such as:
○ $first_double = 123.456;
Booleans
● Booleans are true-or-false values, which are used in control constructs like
the testing portion of an if statement.
NULL
● The type NULL has only one possible value, which is the value NULL.
● To give a variable the NULL value, simply assign it like this:
● $my_var = NULL;
The Simple Types
Strings
● Strings are character sequences, as in the following:
$string_1 = “This is a string in double quotes.”;
● Everything inside quotes, single (‘ ‘) and double (” “) in PHP is treated as a string.
Single-quote strings:This type of string does not process special characters inside
quotes.
<?php
// single-quote strings
$site = 'GeeksforGeeks';
echo 'Welcome to $site';?>
Output: Welcome to $site
The Simple Types
Double-quote strings
Unlike single-quote strings, double-quote strings in PHP are capable of processing
special characters.
<?php
// double-quote strings
echo "Welcome to GeeksforGeeks n";
$site = "GeeksforGeeks";
echo "Welcome to $site";
?>
Output: Welcome to GeeksforGeeks
Output
Echo and print
● The two most basic constructs for printing to output are echo and print
Echo
● The echo statement can be used with or without parentheses: echo or echo().
● The simplest use of echo is to print a string as argument,
● for example:
○ echo “This will print in the user’s browser window.”;
○ echo "This ", "string ", "was ", "made ", "with multiple parameters.";
● Or equivalently:
○ echo(“This will print in the user’s browser window.”);
Output
Print
● The print statement can be used with or without parentheses: print or
print().
● for example:
○ print "Hello world!<br>";
○ print "I'm about to learn PHP!";
● Or equivalently:
○ print ("Hello world!<br>");
○ print ("I'm about to learn PHP!");
Output
Difference between Echo and Print
● echo has no return value while print has a return value of 1 so it can be
used in expressions.
● echo can take multiple parameters (although such usage is rare) while
print can take one argument. echo is marginally faster than print.
Expressions
Boolean Expressions
● Every control structure in this chapter has two distinct parts:
○ the test (which determines which part of the rest of the structure executes), and
○ the dependent code itself (whether separate branches or the body of a loop).
● Tests work by evaluating a Boolean expression, an expression with a
result treated as either true or false.
Boolean constants
● The simplest kind of expression is a simple value, and the simplest
Boolean values are the constants TRUE and FALSE
Logical operators
● Logical operators combine other logical (aka Boolean) values to produce new
Boolean values.
● The standard logical operations (and, or, not, and exclusive-or) are supported
by PHP
Precedence of logical operators
● The logical operators listed in declining order of precedence are: !, &&, ||,
and, xor, or
● Actually, and, xor, and or have much lower precedence than the others, so
that the assignment operator (=) binds more tightly than and but less tightly
than &&.
Comparison operators
Operator precedence
Comparison operators have higher precedence than Boolean operators
String comparison
The comparison operators may be used to compare strings as well as numbers
Ternary operator
Its job is to take three expressions and use the truth value of the first
expression to decide which of the other two expressions to evaluate and return.
The syntax looks like: testExpression ? yesExpression : noExpression
Operator precedence
Comparison operators have higher precedence than Boolean operators
String comparison
The comparison operators may be used to compare strings as well as numbers
Ternary operator
Its job is to take three expressions and use the truth value of the first
expression to decide which of the other two expressions to evaluate and return.
The syntax looks like: testExpression ? yesExpression : noExpression
Ternary operator example
<html>
<body>
<?php
$a=1;
$b=5;
$z = $a>$b ? "greater" : "smaller";
echo($z);
?>
</body>
</html>
Branching
● The two main structures for branching are if and switch.
● Switch is a useful alternative for certain situations where you want
multiple possible branches based on a single value
If-else
if (test)
statement-1
else
statement-2
<html>
<body>
<?php
$a=10;
$b=20;
if ($a > $b) {
echo "a is greater";
} else {
echo "b is greater";
}
?>
</body>
</html>
Switch
For a specific kind of multiway branching, the switch construct can be useful.
switch(expression)
{
case value-1:
statement-1;
statement-2;
...
[break;]
case value-2:
statement-3;
statement-4;
...
[break;]
...
[default:
default-statement;]
}
<!DOCTYPE html>
<html>
<body>
<?php
$day =3;
switch ($day) {
case 0:
echo "Sunday";
break;
case 1:
echo "Monday";
break;
case 2:
echo "tuesday";
break;
case 3:
echo "wednesday";
break;
case 4:
echo "thursday";
break;
case 5:
echo "friday";
break;
case 6:
echo "saturday";
break;
default:
echo "wrong option!";
}
?>
</body>
</html>
Looping
Loops are used to execute the same block of code again and again, as long
as a certain condition is true.
In PHP, we have the following loop types:
● 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
While
The simplest PHP looping construct is while, which has the following syntax:
while (condition)
Statement
Example:
<html>
<body>
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
doWhile
The do-while construct is similar to while, except that the test happens at the end of the loop.
The syntax is:
do statement
while (expression);
Example:
<html>
<body>
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
For
The most complicated looping construct is for, which has the following syntax:
for (initial-expression;
termination-check;
loop-end-expression)
statement
Example:
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
Break and continue
● The break command exits the innermost loop construct that contains it.
● The continue command skips to the end of the current iteration of the innermost loop
that contains it.
Break:
<html>
<body>
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
</body>
</html>
Continue
<html>
<body>
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
</body>
</html>
Using Functions
● A function is a way of wrapping up a chunk of code and giving that chunk a name, so that you
can use that chunk later in just one line of code. Functions are most useful when you will be
using the code in more than one place
The basic syntax for using (or calling) a function is:
function_name(expression_1, expression_2, ..., expression_n)
Built-in PHP functions
● PHP has over 1000 built-in functions that can be called directly, from within a script, to
perform a specific task.
sqrt(9); // square root function, evaluates to 3
rand(10, 10 + 10); // random number between 10 and 20
strlen(“This has 22 characters”); // returns the number 22
pi(); // returns the approximate value of pi
User Defined 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.
Function definition syntax
function function-name ($argument-1, $argument-2, ..)
{
statement-1;
statement-2;
...}
That is, function definitions have four parts:
● The special word function
● The name that you want to give your function
● The function’s parameter list — dollar-sign variables separated by
commas
● The function body — a brace-enclosed set of statements
when a user-defined function is called is:
● PHP looks up the function by its name
● PHP substitutes the values of the calling arguments
● The statements in the body of the function are executed.
Example
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg();
?>
Addition of two numbers
<html>
<body>
<?php
$a=5;
$b=8;
function add($a,$b)
{
$c=$a+$b;
echo $c;
}
add($a,$b);
?>
</body>
</html>
Formal parameters versus actual parameters
● The Actual parameters are the values that are passed to the function when it is
invoked.
● The Formal Parameters are the variables defined by the function that receives
values when the function is called.
Argument number mismatches
What happens if you call a function with fewer arguments than appear in the definition,
or with more? As you might have come to expect by now, PHP handles this without
anything crashing, but it may print a warning depending on your settings for error
reporting.
Functions and Variable Scope
● The scope of a variable is defined as its range in the program under which it can
be accessed.
● In other words, "The scope of a variable is the portion of the program within
which it is defined and can be accessed."
PHP has three types of variable scopes:
1. Local variable
2. Global variable
3. Static variable
Local variable
● The variables that are declared within a function are called local variables for that
function.
● These local variables have their scope only in that particular function in which they
are declared.
● This means that these variables cannot be accessed outside the function, as they
have local scope.
Example:
<?php
function localvar()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
localvar();
?>
Output:Local variable declared inside the function is: 45
Global variable
● The global variables are the variables that are declared outside the function. These
variables can be accessed anywhere in the program.
● To access the global variable within a function, use the GLOBAL keyword before the
variable.
● However, these variables can be directly accessed or used outside the function without
any keyword. Therefore there is no need to use any keyword to access a global variable
outside the function.
Example:
<html>
<body>
<?php
$x=6;// global variable
function globalvar()
{
global $x;
echo "global variable".$x;
}
globalvar();
?>
</body>
</html>
Output:global variable6
Static variable
● It is a feature of PHP to delete the variable, once it completes its execution
and memory is freed. Sometimes we need to store a variable even after
completion of function execution. Therefore, another important feature of
variable scoping is static variable.
● We use the static keyword before the variable to define a variable, and this
variable is called as static variable.
● Static variables exist only in a local function, but it does not free its memory
after the program execution leaves the scope. Understand it with the help of
an example:
Example:
<?php
function staticvar()
{
static $num1 = 3; //static variable
$num2 = 6; //Non-static variable
//increment in non-static variable
$num1++;
//increment in static variable
$num2++;
echo "Static: " .$num1 ."</br>";
echo "Non-static: " .$num2 ."</br>";
}
/first function call
staticvar();
//second function call
staticvar();
?>
Output:
Static: 4
Non-static: 7
Static: 5
Non-static: 7
Function Scope
Include and Require
● It is possible to insert the content of one PHP file into another PHP file (before the server
executes it), with the include or require statement.
● Syntax:
include 'filename';
Or
require 'filename';
Include
A.php
<?php
$color='red';
$car='BMW';
?>
B.php
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php
include 'a.php';
echo "I have a $color $car.";
?>
</body>
</html>
Require
A.php
<?php
$color='red';
$car='BMW';
?>
B.php
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php
require 'a.php';
echo "I have a $color $car.";
?>
</body>
</html>
Include Versus Require
● Use require when the file is required by the application.
● Use include when the file is not required and application should continue when file
is not found.
Include(if file is not found)
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>
</body>
</html>
Output:
Welcome to my home page!
I have a .
require(if file is not found)
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>
</body>
</html>
Output:
Welcome to my home page!
Recursion
PHP also supports recursive function call like C/C++. In such case, we call current
function within function. It is also known as recursion.
Example:
<?php
function display($number) {
if($number<=5){
echo "$number <br/>";
display($number+1);
}
}
display(1);
?>
Output:
1
2
3
4
5

More Related Content

Similar to UNIT 1 (7).pptx

Similar to UNIT 1 (7).pptx (20)

php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
Php web development
Php web developmentPhp web development
Php web development
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Php modul-1
Php modul-1Php modul-1
Php modul-1
 
Php
PhpPhp
Php
 
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHP
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)
 
PHPVariables_075026.ppt
PHPVariables_075026.pptPHPVariables_075026.ppt
PHPVariables_075026.ppt
 
Python-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxPython-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptx
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
chapter Two Server-side Script lang.pptx
chapter  Two Server-side Script lang.pptxchapter  Two Server-side Script lang.pptx
chapter Two Server-side Script lang.pptx
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 

More from DrDhivyaaCRAssistant (15)

UNIT 1 (8).pptx
UNIT 1 (8).pptxUNIT 1 (8).pptx
UNIT 1 (8).pptx
 
Unit – II (1).pptx
Unit – II (1).pptxUnit – II (1).pptx
Unit – II (1).pptx
 
UNIT 1 (7).pptx
UNIT 1 (7).pptxUNIT 1 (7).pptx
UNIT 1 (7).pptx
 
UNIT V (5).pptx
UNIT V (5).pptxUNIT V (5).pptx
UNIT V (5).pptx
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT I (6).pptx
UNIT I (6).pptxUNIT I (6).pptx
UNIT I (6).pptx
 
UNIT V (5).pptx
UNIT V (5).pptxUNIT V (5).pptx
UNIT V (5).pptx
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT 1 (7).pptx
UNIT 1 (7).pptxUNIT 1 (7).pptx
UNIT 1 (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 

Recently uploaded

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

UNIT 1 (7).pptx

  • 1. 18IST61 – OPEN SOURCE SYSTEMS Unit – I Basics of PHP
  • 2. Contents PHP’s Syntax – Comments – Variables – Types in PHP – The Simple Types – Doubles – Booleans – NULL – Strings – Output – Expressions – Branching – Looping – Using Functions – User Defined Functions – Functions and Variable Scope – Function Scope
  • 3. What is PHP? ● PHP is the web development language written by and for web developers. ● PHP stands for PHP: Hypertext Preprocessor ● The product was originally named Personal Home Page ● PHP is a server-side scripting language, usually used to create web applications in combination with a web server, such as Apache ● PHP is a widely-used, open source scripting language ● Embedded within PHP ● Develop dynamic web sites ● Easy integration with DB ● Cross-platform compatibility
  • 4. PHP’s Syntax ● A PHP script can be placed anywhere in the document. ● A PHP script starts with <?php and ends with ?>: <?php // PHP code goes here ?> ● The default file extension for PHP files is ".php". ● A PHP file normally contains HTML tags, and some PHP scripting code.
  • 5. Sample Program <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
  • 6. PHP is whitespace insensitive For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable $four is equivalent: $four = 2 + 2; // single spaces $four <tab>=<tab>2<tab>+<tab>2 ; // spaces and tabs $four = 2 + 2; // multiple lines
  • 7. PHP is sometimes case sensitive In particular, all variables are case sensitive. If you embed the following code in an HTML page: <?php $capital = 67; print(“Variable capital is $capital<BR>”); print(“Variable CaPiTaL is $CaPiTaL<BR>”); ?> The output you will see is: Variable capital is 67 Variable CaPiTaL is
  • 8. Statements are expressions terminated by semicolons ● A statement in PHP is any expression that is followed by a semicolon (;). ● Example: $greeting = “Welcome to PHP!”;
  • 9. Comments A comment is the portion of a program that exists only for the human reader. ● C-style multiline comments ○ The multiline style of commenting is the same as in C: A comment starts with the character pair /* and terminates with the character pair */. ○ For example: /* This is a comment in PHP */ ● Single-line comments: # and // ○ PHP supports two different ways of commenting to the end of a given line: one inherited from C++ and Java and the other from Perl and shell scripts. ○ The shell-script-style comment starts with a pound sign, whereas the C++ style comment starts with two forward slashes. ○ Example: # This is a comment // This is a comment too
  • 10. Variables ● Variables are "containers" for storing information. ● All variables in PHP are denoted with a leading dollar sign ($).The value of a variable is the value of its most recent assignment. ● Variables are assigned with the = operator, with the variable on the left-hand side and the ● expression to be evaluated on the right. ● Variables can, but do not need, to be declared before assignment. ● Variables have no intrinsic type other than the type of their current value. ● Variables used before they are assigned have default values.
  • 11. ● PHP variables are Perl-like ○ All variables in PHP start with a leading $ sign just like scalar variables in the Perl scripting language ● Declaring variables (or not) ○ In PHP, types are associated with values rather than variables, no such declaration is necessary ○ The first step in using a variable is to assign it a value. ● Assigning variables ○ Variable assignment is simple — just write the variable name, and add a single equal sign (=); then add the expression that you want to assign to that variable: $pi = 3 + 0.14159;.
  • 12. ● Reassigning variables ○ There is no interesting distinction in PHP between assigning a variable for the first time and changing its value later ○ Example :$my_num_var = “This should be a number – hope it’s reassigned”; $my_num_var = 5; ● Unassigned variables ○ In a situation where a number is expected, a number will be produced, and this works similarly with character strings. ○ In any context that treats a variable as a number, an unassigned variable will be evaluated as 0; in any context that expects a string value, an unassigned variable will be the empty string
  • 13. ● Variable scope ○ In PHP, variables can be declared anywhere in the script. ○ The scope of a variable is the part of the script where the variable can be referenced/used. ○ PHP has two different variable scopes: ● Local A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function ● Global A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function
  • 14. Types in PHP ● PHP’s type system is simple, streamlined, and flexible, and it insulates the programmer from low-level details. ● PHP makes it easy not to worry too much about typing of variables and values, both because it does not require variables to be typed and because it handles a lot of type conversions No variable type declarations ● The type of a variable does not need to be declared in advance Automatic type conversion ● PHP does a good job of automatically converting types when necessary ● $pi = 3 + 0.14159; ● The result of the expression is a floating-point (double) number, with the integer 3 implicitly converted into floating point before the addition is performed.
  • 15. Type Summary ● Integers are whole numbers, without a decimal point, like 495. ● Doubles are floating-point numbers, like 3.14159 or 49.0. ● Booleans have only two possible values: TRUE and FALSE. ● NULL is a special type that only has one value: NULL. ● Strings are sequences of characters, like ‘PHP 4.0 supports string operations.’ ● Arrays are named and indexed collections of other values. ● Objects are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class. ● Resources are special variables that hold references to resources external to PHP (such as database connections).
  • 16. The Simple Types ● The most of the simple types in PHP (integers, doubles, Booleans, NULL, and strings) should be familiar to those with programming experience Integers ● Integers are the simplest type — they correspond to simple whole numbers, both positive and negative. ● Integers can be assigned to variables, or they can be used in expressions, like this: ○ $int_var = 12345; ○ $another_int = -12345 + 12345;
  • 17. The Simple Types Doubles ● Doubles are floating-point numbers, such as: ○ $first_double = 123.456; Booleans ● Booleans are true-or-false values, which are used in control constructs like the testing portion of an if statement. NULL ● The type NULL has only one possible value, which is the value NULL. ● To give a variable the NULL value, simply assign it like this: ● $my_var = NULL;
  • 18. The Simple Types Strings ● Strings are character sequences, as in the following: $string_1 = “This is a string in double quotes.”; ● Everything inside quotes, single (‘ ‘) and double (” “) in PHP is treated as a string. Single-quote strings:This type of string does not process special characters inside quotes. <?php // single-quote strings $site = 'GeeksforGeeks'; echo 'Welcome to $site';?> Output: Welcome to $site
  • 19. The Simple Types Double-quote strings Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters. <?php // double-quote strings echo "Welcome to GeeksforGeeks n"; $site = "GeeksforGeeks"; echo "Welcome to $site"; ?> Output: Welcome to GeeksforGeeks
  • 20. Output Echo and print ● The two most basic constructs for printing to output are echo and print Echo ● The echo statement can be used with or without parentheses: echo or echo(). ● The simplest use of echo is to print a string as argument, ● for example: ○ echo “This will print in the user’s browser window.”; ○ echo "This ", "string ", "was ", "made ", "with multiple parameters."; ● Or equivalently: ○ echo(“This will print in the user’s browser window.”);
  • 21. Output Print ● The print statement can be used with or without parentheses: print or print(). ● for example: ○ print "Hello world!<br>"; ○ print "I'm about to learn PHP!"; ● Or equivalently: ○ print ("Hello world!<br>"); ○ print ("I'm about to learn PHP!");
  • 22. Output Difference between Echo and Print ● echo has no return value while print has a return value of 1 so it can be used in expressions. ● echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
  • 23. Expressions Boolean Expressions ● Every control structure in this chapter has two distinct parts: ○ the test (which determines which part of the rest of the structure executes), and ○ the dependent code itself (whether separate branches or the body of a loop). ● Tests work by evaluating a Boolean expression, an expression with a result treated as either true or false. Boolean constants ● The simplest kind of expression is a simple value, and the simplest Boolean values are the constants TRUE and FALSE
  • 24. Logical operators ● Logical operators combine other logical (aka Boolean) values to produce new Boolean values. ● The standard logical operations (and, or, not, and exclusive-or) are supported by PHP
  • 25. Precedence of logical operators ● The logical operators listed in declining order of precedence are: !, &&, ||, and, xor, or ● Actually, and, xor, and or have much lower precedence than the others, so that the assignment operator (=) binds more tightly than and but less tightly than &&. Comparison operators
  • 26. Operator precedence Comparison operators have higher precedence than Boolean operators String comparison The comparison operators may be used to compare strings as well as numbers Ternary operator Its job is to take three expressions and use the truth value of the first expression to decide which of the other two expressions to evaluate and return. The syntax looks like: testExpression ? yesExpression : noExpression
  • 27. Operator precedence Comparison operators have higher precedence than Boolean operators String comparison The comparison operators may be used to compare strings as well as numbers Ternary operator Its job is to take three expressions and use the truth value of the first expression to decide which of the other two expressions to evaluate and return. The syntax looks like: testExpression ? yesExpression : noExpression
  • 28. Ternary operator example <html> <body> <?php $a=1; $b=5; $z = $a>$b ? "greater" : "smaller"; echo($z); ?> </body> </html>
  • 29. Branching ● The two main structures for branching are if and switch. ● Switch is a useful alternative for certain situations where you want multiple possible branches based on a single value If-else if (test) statement-1 else statement-2 <html> <body> <?php $a=10; $b=20; if ($a > $b) { echo "a is greater"; } else { echo "b is greater"; } ?> </body> </html>
  • 30. Switch For a specific kind of multiway branching, the switch construct can be useful. switch(expression) { case value-1: statement-1; statement-2; ... [break;] case value-2: statement-3; statement-4; ... [break;] ... [default: default-statement;] }
  • 31. <!DOCTYPE html> <html> <body> <?php $day =3; switch ($day) { case 0: echo "Sunday"; break; case 1: echo "Monday"; break; case 2: echo "tuesday"; break; case 3: echo "wednesday"; break; case 4: echo "thursday"; break; case 5: echo "friday"; break; case 6: echo "saturday"; break; default: echo "wrong option!"; } ?> </body> </html>
  • 32. Looping Loops are used to execute the same block of code again and again, as long as a certain condition is true. In PHP, we have the following loop types: ● 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
  • 33. While The simplest PHP looping construct is while, which has the following syntax: while (condition) Statement Example: <html> <body> <?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> </body> </html>
  • 34. doWhile The do-while construct is similar to while, except that the test happens at the end of the loop. The syntax is: do statement while (expression); Example: <html> <body> <?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> </body> </html>
  • 35. For The most complicated looping construct is for, which has the following syntax: for (initial-expression; termination-check; loop-end-expression) statement Example: <html> <body> <?php for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?> </body> </html>
  • 36. Break and continue ● The break command exits the innermost loop construct that contains it. ● The continue command skips to the end of the current iteration of the innermost loop that contains it. Break: <html> <body> <?php for ($x = 0; $x < 10; $x++) { if ($x == 4) { break; } echo "The number is: $x <br>"; } ?> </body> </html> Continue <html> <body> <?php for ($x = 0; $x < 10; $x++) { if ($x == 4) { continue; } echo "The number is: $x <br>"; } ?> </body> </html>
  • 37. Using Functions ● A function is a way of wrapping up a chunk of code and giving that chunk a name, so that you can use that chunk later in just one line of code. Functions are most useful when you will be using the code in more than one place The basic syntax for using (or calling) a function is: function_name(expression_1, expression_2, ..., expression_n) Built-in PHP functions ● PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task. sqrt(9); // square root function, evaluates to 3 rand(10, 10 + 10); // random number between 10 and 20 strlen(“This has 22 characters”); // returns the number 22 pi(); // returns the approximate value of pi
  • 38. User Defined 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. Function definition syntax function function-name ($argument-1, $argument-2, ..) { statement-1; statement-2; ...}
  • 39. That is, function definitions have four parts: ● The special word function ● The name that you want to give your function ● The function’s parameter list — dollar-sign variables separated by commas ● The function body — a brace-enclosed set of statements
  • 40. when a user-defined function is called is: ● PHP looks up the function by its name ● PHP substitutes the values of the calling arguments ● The statements in the body of the function are executed.
  • 41. Example <?php function writeMsg() { echo "Hello world!"; } writeMsg(); ?> Addition of two numbers <html> <body> <?php $a=5; $b=8; function add($a,$b) { $c=$a+$b; echo $c; } add($a,$b); ?> </body> </html>
  • 42. Formal parameters versus actual parameters ● The Actual parameters are the values that are passed to the function when it is invoked. ● The Formal Parameters are the variables defined by the function that receives values when the function is called. Argument number mismatches What happens if you call a function with fewer arguments than appear in the definition, or with more? As you might have come to expect by now, PHP handles this without anything crashing, but it may print a warning depending on your settings for error reporting.
  • 43. Functions and Variable Scope ● The scope of a variable is defined as its range in the program under which it can be accessed. ● In other words, "The scope of a variable is the portion of the program within which it is defined and can be accessed." PHP has three types of variable scopes: 1. Local variable 2. Global variable 3. Static variable
  • 44. Local variable ● The variables that are declared within a function are called local variables for that function. ● These local variables have their scope only in that particular function in which they are declared. ● This means that these variables cannot be accessed outside the function, as they have local scope.
  • 45. Example: <?php function localvar() { $num = 45; //local variable echo "Local variable declared inside the function is: ". $num; } localvar(); ?> Output:Local variable declared inside the function is: 45
  • 46. Global variable ● The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. ● To access the global variable within a function, use the GLOBAL keyword before the variable. ● However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.
  • 47. Example: <html> <body> <?php $x=6;// global variable function globalvar() { global $x; echo "global variable".$x; } globalvar(); ?> </body> </html> Output:global variable6
  • 48. Static variable ● It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. Therefore, another important feature of variable scoping is static variable. ● We use the static keyword before the variable to define a variable, and this variable is called as static variable. ● Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:
  • 49. Example: <?php function staticvar() { static $num1 = 3; //static variable $num2 = 6; //Non-static variable //increment in non-static variable $num1++; //increment in static variable $num2++; echo "Static: " .$num1 ."</br>"; echo "Non-static: " .$num2 ."</br>"; } /first function call staticvar(); //second function call staticvar(); ?> Output: Static: 4 Non-static: 7 Static: 5 Non-static: 7
  • 50. Function Scope Include and Require ● It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. ● Syntax: include 'filename'; Or require 'filename';
  • 51. Include A.php <?php $color='red'; $car='BMW'; ?> B.php <html> <body> <h1>Welcome to my home page!</h1> <?php include 'a.php'; echo "I have a $color $car."; ?> </body> </html> Require A.php <?php $color='red'; $car='BMW'; ?> B.php <html> <body> <h1>Welcome to my home page!</h1> <?php require 'a.php'; echo "I have a $color $car."; ?> </body> </html>
  • 52. Include Versus Require ● Use require when the file is required by the application. ● Use include when the file is not required and application should continue when file is not found. Include(if file is not found) <html> <body> <h1>Welcome to my home page!</h1> <?php include 'noFileExists.php'; echo "I have a $color $car."; ?> </body> </html> Output: Welcome to my home page! I have a . require(if file is not found) <html> <body> <h1>Welcome to my home page!</h1> <?php require 'noFileExists.php'; echo "I have a $color $car."; ?> </body> </html> Output: Welcome to my home page!
  • 53. Recursion PHP also supports recursive function call like C/C++. In such case, we call current function within function. It is also known as recursion. Example: <?php function display($number) { if($number<=5){ echo "$number <br/>"; display($number+1); } } display(1); ?> Output: 1 2 3 4 5