Presentation on PHP Basics
Submitted to:
Navjot sir
Submitted by:
Ms. Anmol Rattan
1811591
MCA 5th Semester
Outlines
PHP Basic Syntax
Case Sensitivity in PHP
PHP Printing function
Declaration of variables
Loops in PHP
Functions in PHP
1. This is most commonly used and recommended
form is:
PHP Basic Syntax
<?PHP
PHP CODE GOES IN HERE
?>
2. This is another way to add PHP script(HTML
or Script style tags)
Syntax:-
<script language=“PHP”>
PHP CODE GOES IN HERE
</script>
3. This is called “Short” tags.
i. Syntax:
<?
PHP CODE GOES
HERE
?>
ii. Syntax:
<%
PHP CODE GOES
HERE
%>
Embedding of PHP within HTML
<html>
<body>
<?
// php code goes here
?>
</body>
</html>
Case Sensitivity in PHP
In PHP, Variable names are case-sensitive but
function names are not case sensitive.
• If you defined variable in lowercase, then you
need to use it in lowercase everywhere in
program. If we define a
variable $name = "James"; $NAME will not
work
• If you defined function name in lowercase, but
calling them in uppercase it will work. For
example, If we define function sum() {} then
calling SUM() will also work.
Case sensitive (both
user defined and PHP
defined)
• variables
• constants
• array keys
• class properties
• class constants
• Strings
Case insensitive (both
user defined and PHP
defined)
• Functions Name
• class constructors
• class methods
• Class Names
• keywords and
constructs (if, else,
null, foreach, echo
etc.)
PHP PRINTING FUNCTION
1. print() – The print() is used to create PHP print
statement to print given data to the browser. It is
a PHP language construct and not a function. So,
we can use ‘print’ without parenthesis for
creating a print statement.
Example:
<?PHP
print "Anmol";
//(or)
print("Anmol");
?>
OUTPUT
AnmolAnmol
PHP Echo statement
• It is a construct as like as print(). The difference
is that the echo() will accept multiple data
separated by commas.
• PHP echo statement can be used to print string,
multi line strings, variables, array etc.
EXAMPLE:
ECHO "APPLE","GRAPES“ //INVALID
ECHO ("APPLE","GRAPES"); //VALID
Example:
echo "Apple";
echo("Apple");
Declaration of variables in PHP
• A variable in PHP is a name of memory location
that holds data. A variable is used to store data
temporarily.
• In PHP, a variable is declared using $ sign
followed by variable name .
Syntax :-
$variablename=value;
Rules for declaring a variable in
PHP
• The variables in PHP are started by the dollar
sign($).
• PHP variables must start with letter or underscore
only.
• PHP variable can't be start with numbers and
special symbols.
• The variables names are case sensitive i.e.
$kushal and $KUSHAL are two different
variables for interpreter.
PHP Variable: Declaring string,
integer and float
OUTPUT
string is: hello string
integer is: 200
float is: 44.6
<?php
$str="hello string";
$x=200;
$y=44.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
?>
Loops in PHP
• When we want to execute same code
multiple times then we can put it into a loop block
to avoid code repetition. Loop structure includes
three sections. These are,
•Initialization
•Conditional execution
•Incrementation / decrementation
PHP support four different types of
looping control statements:
1. While Loop
2. Do…While Loop
3. For Loop
4. Foreach Loop
1. While Loop
• while loop is also known a entry control loop.
• In this loop, every time condition is checked and
if condition is true, then block of while loop is
executed.
• Loop will terminate if condition will become
false.
Syntax:
while(condition)
{
code to execute;
}
2. Do…While loop
• It is also known as exit controlled loop as in this
condition is checked after executing the block of
loop.
• Do while loop executes a block of code at least
once .
Syntax:
do
{
//codes
}
while(condition);
3. For Loop
• The for loop is more fast than while loop and do
while loop.
• It has the initialization, conditional statement and
increments/decrements in a single line.
Syntax:
for(intialization;
condiion;incr/decr)
{
//Body of loop;
}
• foreach looping structure is used to iterate through
each element of an array starting from first
element.
• It will depend upon the number of elements of
array that how much time the loop will execute.
• Foreach loop works only on arrays.
4. foreach loop
Example
<?php
$Myarray=array('anmol‘ ,
'rohit‘ ,‘ lokesh');
foreach($Myarray as $value)
{
echo $value."<br>";
}
?>
Output:-
anmol
rohit
lokesh
Functions in PHP
• PHP function is a block of statements that can
be reused number of times in PHP script.
• A function can take argument and can return
value.
Syntax:
function functionName()
{
Code to be executed;
}
Advantages of PHP Functions
• Code Reusability: PHP functions are defined
only once and can be invoked many times.
• Less Code: It saves a lot of code because By the
use of function, you can write the logic only once
and reuse it.
• Easy to understand: it is easier to understand the
flow of the application because every logic is
divided in the form of functions.
PHP Functions Example
Output
Hello PHP
<?php
function sayHello()
{
echo "Hello PHP";
}
sayHello();//calling function
?>
PHP functions with arguments
Output
Hello Sonoo
Hello Vimal
Hello John
<?php
function sayHello($name)
{
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
Function with return value
Output
sum of 2 and 5 is: 7
<?php
function sum($a, $b){
$c=$a+$b;
return $c;
}
$a=2;$b=5;
echo "sum of $a and $b is:
".sum($a,$b);
?>
php basics

php basics

  • 1.
    Presentation on PHPBasics Submitted to: Navjot sir Submitted by: Ms. Anmol Rattan 1811591 MCA 5th Semester
  • 2.
    Outlines PHP Basic Syntax CaseSensitivity in PHP PHP Printing function Declaration of variables Loops in PHP Functions in PHP
  • 3.
    1. This ismost commonly used and recommended form is: PHP Basic Syntax <?PHP PHP CODE GOES IN HERE ?>
  • 4.
    2. This isanother way to add PHP script(HTML or Script style tags) Syntax:- <script language=“PHP”> PHP CODE GOES IN HERE </script>
  • 5.
    3. This iscalled “Short” tags. i. Syntax: <? PHP CODE GOES HERE ?> ii. Syntax: <% PHP CODE GOES HERE %>
  • 6.
    Embedding of PHPwithin HTML <html> <body> <? // php code goes here ?> </body> </html>
  • 7.
    Case Sensitivity inPHP In PHP, Variable names are case-sensitive but function names are not case sensitive. • If you defined variable in lowercase, then you need to use it in lowercase everywhere in program. If we define a variable $name = "James"; $NAME will not work • If you defined function name in lowercase, but calling them in uppercase it will work. For example, If we define function sum() {} then calling SUM() will also work.
  • 8.
    Case sensitive (both userdefined and PHP defined) • variables • constants • array keys • class properties • class constants • Strings Case insensitive (both user defined and PHP defined) • Functions Name • class constructors • class methods • Class Names • keywords and constructs (if, else, null, foreach, echo etc.)
  • 9.
    PHP PRINTING FUNCTION 1.print() – The print() is used to create PHP print statement to print given data to the browser. It is a PHP language construct and not a function. So, we can use ‘print’ without parenthesis for creating a print statement.
  • 10.
  • 11.
    PHP Echo statement •It is a construct as like as print(). The difference is that the echo() will accept multiple data separated by commas. • PHP echo statement can be used to print string, multi line strings, variables, array etc.
  • 12.
    EXAMPLE: ECHO "APPLE","GRAPES“ //INVALID ECHO("APPLE","GRAPES"); //VALID Example: echo "Apple"; echo("Apple");
  • 13.
    Declaration of variablesin PHP • A variable in PHP is a name of memory location that holds data. A variable is used to store data temporarily. • In PHP, a variable is declared using $ sign followed by variable name . Syntax :- $variablename=value;
  • 14.
    Rules for declaringa variable in PHP • The variables in PHP are started by the dollar sign($). • PHP variables must start with letter or underscore only. • PHP variable can't be start with numbers and special symbols. • The variables names are case sensitive i.e. $kushal and $KUSHAL are two different variables for interpreter.
  • 15.
    PHP Variable: Declaringstring, integer and float OUTPUT string is: hello string integer is: 200 float is: 44.6 <?php $str="hello string"; $x=200; $y=44.6; echo "string is: $str <br/>"; echo "integer is: $x <br/>"; echo "float is: $y <br/>"; ?>
  • 16.
    Loops in PHP •When we want to execute same code multiple times then we can put it into a loop block to avoid code repetition. Loop structure includes three sections. These are, •Initialization •Conditional execution •Incrementation / decrementation
  • 17.
    PHP support fourdifferent types of looping control statements: 1. While Loop 2. Do…While Loop 3. For Loop 4. Foreach Loop
  • 18.
    1. While Loop •while loop is also known a entry control loop. • In this loop, every time condition is checked and if condition is true, then block of while loop is executed. • Loop will terminate if condition will become false.
  • 19.
  • 20.
    2. Do…While loop •It is also known as exit controlled loop as in this condition is checked after executing the block of loop. • Do while loop executes a block of code at least once .
  • 21.
  • 22.
    3. For Loop •The for loop is more fast than while loop and do while loop. • It has the initialization, conditional statement and increments/decrements in a single line.
  • 23.
  • 24.
    • foreach loopingstructure is used to iterate through each element of an array starting from first element. • It will depend upon the number of elements of array that how much time the loop will execute. • Foreach loop works only on arrays. 4. foreach loop
  • 25.
    Example <?php $Myarray=array('anmol‘ , 'rohit‘ ,‘lokesh'); foreach($Myarray as $value) { echo $value."<br>"; } ?> Output:- anmol rohit lokesh
  • 26.
    Functions in PHP •PHP function is a block of statements that can be reused number of times in PHP script. • A function can take argument and can return value. Syntax: function functionName() { Code to be executed; }
  • 27.
    Advantages of PHPFunctions • Code Reusability: PHP functions are defined only once and can be invoked many times. • Less Code: It saves a lot of code because By the use of function, you can write the logic only once and reuse it. • Easy to understand: it is easier to understand the flow of the application because every logic is divided in the form of functions.
  • 28.
    PHP Functions Example Output HelloPHP <?php function sayHello() { echo "Hello PHP"; } sayHello();//calling function ?>
  • 29.
    PHP functions witharguments Output Hello Sonoo Hello Vimal Hello John <?php function sayHello($name) { echo "Hello $name<br/>"; } sayHello("Sonoo"); sayHello("Vimal"); sayHello("John"); ?>
  • 30.
    Function with returnvalue Output sum of 2 and 5 is: 7 <?php function sum($a, $b){ $c=$a+$b; return $c; } $a=2;$b=5; echo "sum of $a and $b is: ".sum($a,$b); ?>