SlideShare a Scribd company logo
WEB PROGRAMMING
PHP Basics
S.Muthuganesh M.Sc.,B.Ed
Assistant Professor
Department of Computer Science
Vivekananda College
Tiruvedakam West
ganesh01muthu@gmail.com
Comments
• Giving the name of the program, the author and other details, which the
programmer would like to use.
• Its not executed part and its only for one who looking at the code.
Syntax
// This is the first programme (Single line Comments //)
# Declaring Variable (Single line Comments #)
/*this is block comment
Placed over two or more
Lines*/ (Multiple line comments /*………*/)
Constants
• A constant is an identifier (name) for a simple value. As the name suggests, that
value cannot change during the execution of the script. A constant is case-
sensitive by default should follow the variables rules to define a constant. There
is no need $(dollar symbol).
Syntax
define(“<constantname>”,<expr> [,<casesensitive>]);
Example
define(“stringcons”,”hai welcome”);
define(“intcons”,2000);
define(“pi”,3.14);
Data types
• PHP has several types, all holds specific class of information.
String
Strings are sequences of characters that are always internally NULL terminated.
There is no limit to size Ex $str=”hai how are you?”;
Integer
It holds whole numbers, it can be specified in decimal (base 10), hexadecimal (base
16 - prefixed with 0x) or octal (base 8 - prefixed with 0) notation, optionally
preceded by a sign (- or +). There is a maximum limit from -2147483647(-231) to
+2147483647(+231).
Float
Floating point numbers that holds high integer values or fractional numbers
Ex $pi=3.14;
Data types
Boolean
Booleans are like a switch it has only two possible values either 1 (true) or 0 (false).
Arrays
An array is a variable that can hold more than one value at a time. It is useful to
aggregate a series of related items together.
Ex $weekdays=(“Sunday”,”Monday”,”Tuesday”);
Objects
Object variable are complex variables that holds own properties and methods for
accessing or manipulating their content.
Resource
A resource is a special variable that hold anything that is not PHP data. Resource
variables typically hold special handlers to opened files and database connections.
Variables
• All variables in PHP start with a $ sign, followed by the name of the variable.
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ )
• Variable names are case-sensitive ($name and $NAME are two different
variables)
Declaring variable
$var_name=value;
Ex
$age=19;
$name=“karthick”;
$amount=45.89;
Deliver text as output
• To output text in a PHP script to a browser print or echo is command which tells the
PHP interpreter what do.
Print
Print is also a statement i.e used to display the output. it can be used with
parentheses print( ) or without parentheses print.
using print can doesn't pass multiple argument
it’s slower
ex print(“hai”);
echo
echo is a statement i.e used to display the output. it can be used with parentheses
echo or without parentheses echo.
echo can pass multiple string separated as ( , )
its faster than print
ex echo(“hai”);
Superglobals
• Superglobals are variables do not have a fixed scope they are available in all of
them, they provide as follows
• Useful information about the environment
• Allow to access to HTML form variables or parenthesis
• Access to cookies stored on a client
• Keeping tracking session and file uploads
• $_GET : $_GET is a super global variable used to collect data from the HTML form
after submitting it. $_GET super global array variable stores the values that come
in the URL.
• $_POST: superglobal is needed in order to pass PHP variables or collect data from
forms after they have been submitted via an HTML form. It’s similar to $_GET,
conventionally used the contents of an HTML form are going to change values in
database permanently.
Superglobals
• $_REQUEST: It is a superglobal variable which is used to collect the data after
submitting a HTML form. It's similar to $_GET and $_POST variables. $_REQUEST
is not used mostly, because $_POST and $_GET perform the same task and are
widely used.
• $GLOBALS : It is a superglobal variable which is used to access global variables
from anywhere in the PHP script. PHP stores all the global variables in array
$GLOBALS where index holds the global variable name.
• $_COOKIE – contains all variables passed to HTTP Cookies.
• $_SERVER: set by the web server or otherwise directly related to current
environment the script.
Superglobals
• $_SESSION – holds the variables which are currently registered script.
• $_ENV - An associative of variables passed to the current script via the
environment method.
• $_FILES – holds variables are uploaded to the current script via the HTTP POST
method.
Here Documents
• Here documents a special form of I/O
redirection to feed command list to an
interactive program or command.
• you need to set in your PHP script a string
containing big chunk of text data (for
example, HTML code), it is easier to use
"here document" style.
• This special way allows you to set multiline
strings, with all included variables
expanded as it is done in double quotes
style.
• Save as
C:xampphtdocsprogramsheredoc.php
• Start Apache server
• Run in any Browser
http://localhost/programs/heredoc.php
<?php
$heremes=<<<EOF
<b>list of books</b>
<ol>
<li>java
<li>Programming in C
<li>OOP with C++
<li>PHP and HTML
</ol>
EOF;
echo($heremes);
?>
Operators
• Operators is any symbol used to perform an operation on a value. PHP operator
classified into
• Unary operator
• Binary operator
• Ternary operator
Unary operator
It works on only one operand
Operator Meaning Description
! Logical Negation True if the operand evaluates to false
False if the operand evaluates to true
~ Bitwise negation Bitwise representation
Unary operator – increment and decrement
• Increment ++ increases the value by 1 whereas decrement -- decreases the value
by 1.
• ++ operator as prefix like: ++$var. The value of var is incremented by 1 then, it
returns the value.
• use ++ operator as postfix like: $var++. The original value of var is returned first
then, var is incremented by 1.
• -- operator as prefix like: --$var. The value of var is decremented by 1 then, it
returns the value.
• use -- operator as postfix like: $var--. The original value of var is returned first
then, var is decremented by 1.
Cast operator
• Way to force a type conversion of value using the cast operators. The operand
appear on the right side of the cast operator and its result is converted type.
$val=3.14;
$valint=(int)$val;
It returns 3
Operator Changes type to
int or integer Integer
float, real or double Floating point
string String
bool or boolean Boolean
array Array
object object
Arithmetic Operator
• Here are following arithmetic operators supported by PHP language −Assume
variable A holds 10 and variable B holds 5 then −
Operator Description Example
+ Adds two operands $A + $B will give 15
- Subtracts second operand from the first $A - $B will give 5
* Multiply both operands $A * $B will give 50
/ Divide numerator by de-numerator $A / $B will give 2
% Modulus Operator and remainder of after an integer division $A % $B will give 0
++ Increment operator, increases integer value by one $A++ will give 11
-- Decrement operator, decreases integer value by one $A-- will give 9
Assignment operator
• The assignment operators are used to assign values result of an expression to a variable Assigns
values from right side operands to left side operand
Opera
tor
Description Example
= Simple assignment operator. $C = $A + $B will assign the value of $A + $B to $C
+= Add AND assignment operator. $C+= $A is equivalent to $C = $C + $A
-= Subtract AND assignment operator. $C -= $A is equivalent to $C = $C - $A
*= Multiply AND assignment operator. $C *= $A is equivalent to $C = $C * $A
/= Divide AND assignment operator. $C /= $A is equivalent to $C = $C / $A
%= Modulus AND assignment operator. $C %= $A is equivalent to $C = $C % $A
Comparison operator
• We often compare two quantities and depending on their relation, take certain
decisions
Operator Description
== 3==9 returns false
!= 3!=9 returns true.
> 3>9 returns false
< 3<9 returns true
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition
becomes true. 15>=18 returns false
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition
becomes true. 15<=18 returns true
=== Identical that means 3===3 returns true
Concatenates
• The concatenation operator (‘.‘), which returns the concatenation of its right and
left arguments (join two arguments).
<?php
$a=10;
$b=20;
$c=$a+$b;
echo ”the addition of value of c=“.$c;
?>
OUTPUT
the addition of c=30
Logical operator
• The logical operators are used when we want, test more than one condition
Operator Description
&&, and Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
||,or,|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
Bitwise operator
• The Bitwise operators is used to perform Bit-level operations on the operands.
The operators are first converted to bit-level (Binary Digit) and then calculation is
performed on the operands.
Operator Meaning
Bitwise AND & This is a binary operator. Bitwise AND operator in PHP takes two numbers as operands and does AND
on every bit of two numbers. The result of AND is 1 only if both bits are 1.
Bitwise OR | This is also binary operator. Bitwise OR operator takes two numbers as operands and does OR on every
bit of two numbers. The result of OR is 1 any of the two bits is 1.
Bitwise XOR ^ This is also binary operator. Bitwise XOR takes two numbers as operands and does XOR on every bit of
two numbers. The result of XOR is 1 if the two bits are different.
Numbers/
Bit level
23=8 22=4 21=2 20=1 Result
4 0 1 0 0
5 0 1 0 1
Bitwise AND & 0 1 0 0 4
Bitwise OR | 0 1 0 1 5
Bitwise OR ^ 0 0 0 1 1
Ternary Operator (Conditional Operator)
• In simple words, PHP Ternary Operator is a shortened method of writing an if-
else statement.
Syntax
$condition ? 'true result' : 'false result';
Example
<?php
$n=11;
$ans=($n%2==0)?"even number":"odd number";
echo $ans;
?>
Output
odd number
Reference Operator
• When the = operator is used, PHP performs a copy operation.
• References allow two variables to refer to the same content.
• The ampersand (&) is placed before the variable to be referenced.
• Once two variables are pointing to the same data, either of variables can be
changed and the other one will also be updated.
Reference Operator
Example
<?php
$a=5;
$b=&$a;
echo"before update a=$a"."<br>";
echo"before update b=$b"."<br>";
$a++;
echo "After update a=$a and b=$b"."<br>";
$b++;
echo "now b update a=$a and b=$b"."<br>";
?>
Thank you

More Related Content

What's hot

Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2Kumar
 
Functions in php
Functions in phpFunctions in php
Functions in php
Mudasir Syed
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
tumetr1
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Php basics
Php basicsPhp basics
Php basics
Hewitt VS
 
Introduction to web and php mysql
Introduction to web and php mysqlIntroduction to web and php mysql
Introduction to web and php mysql
Programmer Blog
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
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 Kianphelios
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
Jalpesh Vasa
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
Prof. Wim Van Criekinge
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
Nithin Kumar Singani
 
Introduction to php php++
Introduction to php php++Introduction to php php++
Introduction to php php++
Tanay Kishore Mishra
 
PHP - Introduction to PHP Date and Time Functions
PHP -  Introduction to  PHP Date and Time FunctionsPHP -  Introduction to  PHP Date and Time Functions
PHP - Introduction to PHP Date and Time Functions
Vibrant Technologies & Computers
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
SHIVANI SONI
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
Manav Prasad
 

What's hot (20)

Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Php basics
Php basicsPhp basics
Php basics
 
Introduction to web and php mysql
Introduction to web and php mysqlIntroduction to web and php mysql
Introduction to web and php mysql
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
 
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
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Introduction to php php++
Introduction to php php++Introduction to php php++
Introduction to php php++
 
PHP - Introduction to PHP Date and Time Functions
PHP -  Introduction to  PHP Date and Time FunctionsPHP -  Introduction to  PHP Date and Time Functions
PHP - Introduction to PHP Date and Time Functions
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 

Similar to PHP Basics

Intro to php
Intro to phpIntro to php
Intro to php
NithyaNithyav
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
Japneet9
 
Introduction to PHP_Slides by Lesley_Bonyo.pdf
Introduction to PHP_Slides by Lesley_Bonyo.pdfIntroduction to PHP_Slides by Lesley_Bonyo.pdf
Introduction to PHP_Slides by Lesley_Bonyo.pdf
MacSila
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
Yoeung Vibol
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
pkaviya
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
php 1
php 1php 1
php 1
tumetr1
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
sami2244
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erick
okelloerick
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
Chris Chubb
 
Php introduction
Php introductionPhp introduction
Php introduction
Pratik Patel
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
Ahmed Swilam
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
adityathote3
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuationlotlot
 
Php assignment help
Php assignment helpPhp assignment help
Php assignment help
Jacob William
 

Similar to PHP Basics (20)

Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
Introduction to PHP_Slides by Lesley_Bonyo.pdf
Introduction to PHP_Slides by Lesley_Bonyo.pdfIntroduction to PHP_Slides by Lesley_Bonyo.pdf
Introduction to PHP_Slides by Lesley_Bonyo.pdf
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
php 1
php 1php 1
php 1
 
Php essentials
Php essentialsPhp essentials
Php essentials
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Php basics
Php basicsPhp basics
Php basics
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erick
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuation
 
Php assignment help
Php assignment helpPhp assignment help
Php assignment help
 

More from Muthuganesh S

javascript.pptx
javascript.pptxjavascript.pptx
javascript.pptx
Muthuganesh S
 
OR
OROR
Operation Research VS Software Engineering
Operation Research VS Software EngineeringOperation Research VS Software Engineering
Operation Research VS Software Engineering
Muthuganesh S
 
Cnotes
CnotesCnotes
CSS
CSSCSS
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
Muthuganesh S
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S
 
Php notes
Php notesPhp notes
Php notes
Muthuganesh S
 
Php Basics Iterations, looping
Php Basics Iterations, loopingPhp Basics Iterations, looping
Php Basics Iterations, looping
Muthuganesh S
 
Php
PhpPhp
Introduction to c
Introduction to cIntroduction to c
Introduction to c
Muthuganesh S
 
Javascript dom
Javascript domJavascript dom
Javascript dom
Muthuganesh S
 

More from Muthuganesh S (12)

javascript.pptx
javascript.pptxjavascript.pptx
javascript.pptx
 
OR
OROR
OR
 
Operation Research VS Software Engineering
Operation Research VS Software EngineeringOperation Research VS Software Engineering
Operation Research VS Software Engineering
 
Cnotes
CnotesCnotes
Cnotes
 
CSS
CSSCSS
CSS
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Php notes
Php notesPhp notes
Php notes
 
Php Basics Iterations, looping
Php Basics Iterations, loopingPhp Basics Iterations, looping
Php Basics Iterations, looping
 
Php
PhpPhp
Php
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Javascript dom
Javascript domJavascript dom
Javascript dom
 

Recently uploaded

Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

PHP Basics

  • 1. WEB PROGRAMMING PHP Basics S.Muthuganesh M.Sc.,B.Ed Assistant Professor Department of Computer Science Vivekananda College Tiruvedakam West ganesh01muthu@gmail.com
  • 2. Comments • Giving the name of the program, the author and other details, which the programmer would like to use. • Its not executed part and its only for one who looking at the code. Syntax // This is the first programme (Single line Comments //) # Declaring Variable (Single line Comments #) /*this is block comment Placed over two or more Lines*/ (Multiple line comments /*………*/)
  • 3. Constants • A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script. A constant is case- sensitive by default should follow the variables rules to define a constant. There is no need $(dollar symbol). Syntax define(“<constantname>”,<expr> [,<casesensitive>]); Example define(“stringcons”,”hai welcome”); define(“intcons”,2000); define(“pi”,3.14);
  • 4. Data types • PHP has several types, all holds specific class of information. String Strings are sequences of characters that are always internally NULL terminated. There is no limit to size Ex $str=”hai how are you?”; Integer It holds whole numbers, it can be specified in decimal (base 10), hexadecimal (base 16 - prefixed with 0x) or octal (base 8 - prefixed with 0) notation, optionally preceded by a sign (- or +). There is a maximum limit from -2147483647(-231) to +2147483647(+231). Float Floating point numbers that holds high integer values or fractional numbers Ex $pi=3.14;
  • 5. Data types Boolean Booleans are like a switch it has only two possible values either 1 (true) or 0 (false). Arrays An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together. Ex $weekdays=(“Sunday”,”Monday”,”Tuesday”); Objects Object variable are complex variables that holds own properties and methods for accessing or manipulating their content. Resource A resource is a special variable that hold anything that is not PHP data. Resource variables typically hold special handlers to opened files and database connections.
  • 6. Variables • All variables in PHP start with a $ sign, followed by the name of the variable. • A variable name must start with a letter or the underscore character • A variable name cannot start with a number • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) • Variable names are case-sensitive ($name and $NAME are two different variables) Declaring variable $var_name=value; Ex $age=19; $name=“karthick”; $amount=45.89;
  • 7. Deliver text as output • To output text in a PHP script to a browser print or echo is command which tells the PHP interpreter what do. Print Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print. using print can doesn't pass multiple argument it’s slower ex print(“hai”); echo echo is a statement i.e used to display the output. it can be used with parentheses echo or without parentheses echo. echo can pass multiple string separated as ( , ) its faster than print ex echo(“hai”);
  • 8. Superglobals • Superglobals are variables do not have a fixed scope they are available in all of them, they provide as follows • Useful information about the environment • Allow to access to HTML form variables or parenthesis • Access to cookies stored on a client • Keeping tracking session and file uploads • $_GET : $_GET is a super global variable used to collect data from the HTML form after submitting it. $_GET super global array variable stores the values that come in the URL. • $_POST: superglobal is needed in order to pass PHP variables or collect data from forms after they have been submitted via an HTML form. It’s similar to $_GET, conventionally used the contents of an HTML form are going to change values in database permanently.
  • 9. Superglobals • $_REQUEST: It is a superglobal variable which is used to collect the data after submitting a HTML form. It's similar to $_GET and $_POST variables. $_REQUEST is not used mostly, because $_POST and $_GET perform the same task and are widely used. • $GLOBALS : It is a superglobal variable which is used to access global variables from anywhere in the PHP script. PHP stores all the global variables in array $GLOBALS where index holds the global variable name. • $_COOKIE – contains all variables passed to HTTP Cookies. • $_SERVER: set by the web server or otherwise directly related to current environment the script.
  • 10. Superglobals • $_SESSION – holds the variables which are currently registered script. • $_ENV - An associative of variables passed to the current script via the environment method. • $_FILES – holds variables are uploaded to the current script via the HTTP POST method.
  • 11. Here Documents • Here documents a special form of I/O redirection to feed command list to an interactive program or command. • you need to set in your PHP script a string containing big chunk of text data (for example, HTML code), it is easier to use "here document" style. • This special way allows you to set multiline strings, with all included variables expanded as it is done in double quotes style. • Save as C:xampphtdocsprogramsheredoc.php • Start Apache server • Run in any Browser http://localhost/programs/heredoc.php <?php $heremes=<<<EOF <b>list of books</b> <ol> <li>java <li>Programming in C <li>OOP with C++ <li>PHP and HTML </ol> EOF; echo($heremes); ?>
  • 12. Operators • Operators is any symbol used to perform an operation on a value. PHP operator classified into • Unary operator • Binary operator • Ternary operator Unary operator It works on only one operand Operator Meaning Description ! Logical Negation True if the operand evaluates to false False if the operand evaluates to true ~ Bitwise negation Bitwise representation
  • 13. Unary operator – increment and decrement • Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. • ++ operator as prefix like: ++$var. The value of var is incremented by 1 then, it returns the value. • use ++ operator as postfix like: $var++. The original value of var is returned first then, var is incremented by 1. • -- operator as prefix like: --$var. The value of var is decremented by 1 then, it returns the value. • use -- operator as postfix like: $var--. The original value of var is returned first then, var is decremented by 1.
  • 14. Cast operator • Way to force a type conversion of value using the cast operators. The operand appear on the right side of the cast operator and its result is converted type. $val=3.14; $valint=(int)$val; It returns 3 Operator Changes type to int or integer Integer float, real or double Floating point string String bool or boolean Boolean array Array object object
  • 15. Arithmetic Operator • Here are following arithmetic operators supported by PHP language −Assume variable A holds 10 and variable B holds 5 then − Operator Description Example + Adds two operands $A + $B will give 15 - Subtracts second operand from the first $A - $B will give 5 * Multiply both operands $A * $B will give 50 / Divide numerator by de-numerator $A / $B will give 2 % Modulus Operator and remainder of after an integer division $A % $B will give 0 ++ Increment operator, increases integer value by one $A++ will give 11 -- Decrement operator, decreases integer value by one $A-- will give 9
  • 16. Assignment operator • The assignment operators are used to assign values result of an expression to a variable Assigns values from right side operands to left side operand Opera tor Description Example = Simple assignment operator. $C = $A + $B will assign the value of $A + $B to $C += Add AND assignment operator. $C+= $A is equivalent to $C = $C + $A -= Subtract AND assignment operator. $C -= $A is equivalent to $C = $C - $A *= Multiply AND assignment operator. $C *= $A is equivalent to $C = $C * $A /= Divide AND assignment operator. $C /= $A is equivalent to $C = $C / $A %= Modulus AND assignment operator. $C %= $A is equivalent to $C = $C % $A
  • 17. Comparison operator • We often compare two quantities and depending on their relation, take certain decisions Operator Description == 3==9 returns false != 3!=9 returns true. > 3>9 returns false < 3<9 returns true >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. 15>=18 returns false <= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. 15<=18 returns true === Identical that means 3===3 returns true
  • 18. Concatenates • The concatenation operator (‘.‘), which returns the concatenation of its right and left arguments (join two arguments). <?php $a=10; $b=20; $c=$a+$b; echo ”the addition of value of c=“.$c; ?> OUTPUT the addition of c=30
  • 19. Logical operator • The logical operators are used when we want, test more than one condition Operator Description &&, and Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. ||,or,|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. ! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
  • 20. Bitwise operator • The Bitwise operators is used to perform Bit-level operations on the operands. The operators are first converted to bit-level (Binary Digit) and then calculation is performed on the operands. Operator Meaning Bitwise AND & This is a binary operator. Bitwise AND operator in PHP takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Bitwise OR | This is also binary operator. Bitwise OR operator takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1. Bitwise XOR ^ This is also binary operator. Bitwise XOR takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. Numbers/ Bit level 23=8 22=4 21=2 20=1 Result 4 0 1 0 0 5 0 1 0 1 Bitwise AND & 0 1 0 0 4 Bitwise OR | 0 1 0 1 5 Bitwise OR ^ 0 0 0 1 1
  • 21. Ternary Operator (Conditional Operator) • In simple words, PHP Ternary Operator is a shortened method of writing an if- else statement. Syntax $condition ? 'true result' : 'false result'; Example <?php $n=11; $ans=($n%2==0)?"even number":"odd number"; echo $ans; ?> Output odd number
  • 22. Reference Operator • When the = operator is used, PHP performs a copy operation. • References allow two variables to refer to the same content. • The ampersand (&) is placed before the variable to be referenced. • Once two variables are pointing to the same data, either of variables can be changed and the other one will also be updated.
  • 23. Reference Operator Example <?php $a=5; $b=&$a; echo"before update a=$a"."<br>"; echo"before update b=$b"."<br>"; $a++; echo "After update a=$a and b=$b"."<br>"; $b++; echo "now b update a=$a and b=$b"."<br>"; ?>