SlideShare a Scribd company logo
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 Basics of PHP by Dr.C.R.Dhivyaa Kongu Engineering College

php basic part one
php basic part onephp basic part one
php basic part one
jeweltutin
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
HambaAbebe2
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
Robby Firmansyah
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
Nisa Soomro
 
Php web development
Php web developmentPhp web development
Php web development
Ramesh Gupta
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
Devashish Kumar
 
Php
PhpPhp
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHP
Deo Shao
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)
Coder Tech
 
PHPVariables_075026.ppt
PHPVariables_075026.pptPHPVariables_075026.ppt
PHPVariables_075026.ppt
06Vinit
 
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
muzammildev46gmailco
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
TKSanthoshRao
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
sami2244
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)Arjun Shanka
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
rasool noorpour
 
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
alehegn9
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
Mohammed Ilyas
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
KIRAN KUMAR SILIVERI
 

Similar to Basics of PHP by Dr.C.R.Dhivyaa Kongu Engineering College (20)

php basic part one
php basic part onephp basic part one
php basic part one
 
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 tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
 
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
 

More from Dhivyaa C.R

Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering CollegeDatabase Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering CollegeLearning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...
Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...
Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...
Dhivyaa C.R
 
Artificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering College
Artificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering CollegeArtificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering College
Artificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering CollegeBayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Machine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering College
Machine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering CollegeMachine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering College
Machine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Unit v -Construction and Evaluation
Unit v -Construction and EvaluationUnit v -Construction and Evaluation
Unit v -Construction and Evaluation
Dhivyaa C.R
 
Unit iv -Documenting and Implementation of Software Architecture
Unit iv -Documenting and Implementation of Software ArchitectureUnit iv -Documenting and Implementation of Software Architecture
Unit iv -Documenting and Implementation of Software Architecture
Dhivyaa C.R
 
Unit iii-Architecture in the lifecycle
Unit iii-Architecture in the lifecycleUnit iii-Architecture in the lifecycle
Unit iii-Architecture in the lifecycle
Dhivyaa C.R
 
Unit v-Distributed Transaction and Replication
Unit v-Distributed Transaction and ReplicationUnit v-Distributed Transaction and Replication
Unit v-Distributed Transaction and Replication
Dhivyaa C.R
 
Unit iv -Transactions
Unit iv -TransactionsUnit iv -Transactions
Unit iv -Transactions
Dhivyaa C.R
 
Unit iii-Synchronization
Unit iii-SynchronizationUnit iii-Synchronization
Unit iii-Synchronization
Dhivyaa C.R
 
Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...
Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...
Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...
Dhivyaa C.R
 
Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...
Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...
Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...
Dhivyaa C.R
 
Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...
Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...
Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...
Dhivyaa C.R
 
Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...
Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...
Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...
Dhivyaa C.R
 

More from Dhivyaa C.R (19)

Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering CollegeDatabase Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering CollegeLearning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
 
Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...
Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...
Instance Learning and Genetic Algorithm by Dr.C.R.Dhivyaa Kongu Engineering C...
 
Artificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering College
Artificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering CollegeArtificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering College
Artificial Neural Network by Dr.C.R.Dhivyaa Kongu Engineering College
 
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering CollegeBayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
 
Machine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering College
Machine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering CollegeMachine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering College
Machine Learning Introduction by Dr.C.R.Dhivyaa Kongu Engineering College
 
Unit v -Construction and Evaluation
Unit v -Construction and EvaluationUnit v -Construction and Evaluation
Unit v -Construction and Evaluation
 
Unit iv -Documenting and Implementation of Software Architecture
Unit iv -Documenting and Implementation of Software ArchitectureUnit iv -Documenting and Implementation of Software Architecture
Unit iv -Documenting and Implementation of Software Architecture
 
Unit iii-Architecture in the lifecycle
Unit iii-Architecture in the lifecycleUnit iii-Architecture in the lifecycle
Unit iii-Architecture in the lifecycle
 
Unit v-Distributed Transaction and Replication
Unit v-Distributed Transaction and ReplicationUnit v-Distributed Transaction and Replication
Unit v-Distributed Transaction and Replication
 
Unit iv -Transactions
Unit iv -TransactionsUnit iv -Transactions
Unit iv -Transactions
 
Unit iii-Synchronization
Unit iii-SynchronizationUnit iii-Synchronization
Unit iii-Synchronization
 
Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...
Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...
Inter process communication by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engi...
 
Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...
Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...
Quality attributes in software architecture by Dr.C.R.Dhivyaa, Assistant prof...
 
Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...
Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...
Software architecture by Dr.C.R.Dhivyaa, Assistant Professor,Kongu Engineerin...
 
Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...
Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...
Distributed computing by Dr.C.R.Dhivyaa, Assistant Professor, Kongu Engineeri...
 

Recently uploaded

Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 

Recently uploaded (20)

Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 

Basics of PHP by Dr.C.R.Dhivyaa Kongu Engineering College

  • 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