SlideShare a Scribd company logo
PHP
String Functions
1. String Functions
 A string is a sequence of characters, like "Hello world!".
1) The PHP strlen() function:
 The strlen() function returns the length of a string, in characters.
 The example below returns the length of the string "Hello world!":
Example:
Tip: strlen() is often used in loops or other functions, when it is
important to know when a string ends.
 i.e. in a loop, we might want to stop the loop after the last character in a string.
<?php
echo strlen("Hello world!");
?>
1. String Functions ...
2) The PHP strpos() function
 The strpos() function is used to search for a specified character or text
within a string.
 If a match is found, it will return the character position of the first
match. If no match is found, it will return FALSE.
 The example below searches for the text "world" in the string "Hello
world!":
Example:
Tip: The position of the string "world" in the example above is 6.
 The reason that it is 6 (and not 7), is that the first character position in the string is 0,
and not 1.
<?php
echo strpos("Hello world!","world");
?>
1. String Functions ...
Function Description
addcslashes() Returns a string with backslashes in front of the specified characters
addslashes() Returns a string with backslashes in front of predefined characters
bin2hex() Converts a string of ASCII characters to hexadecimal values
chop() Removes whitespace or other characters from the right end of a string
chr() Returns a character from a specified ASCII value
chunk_split() Splits a string into a series of smaller parts
convert_cyr_string() Converts a string from one Cyrillic character-set to another
convert_uudecode() Decodes a uuencoded string
convert_uuencode() Encodes a string using the uuencode algorithm
count_chars() Returns information about characters used in a string
crc32() Calculates a 32-bit CRC for a string
crypt() One-way string encryption (hashing)
echo() Outputs one or more strings
explode() Breaks a string into an array
fprintf() Writes a formatted string to a specified output stream
get_html_translation_table() Returns the translation table used by htmlspecialchars() and htmlentities()
hebrev() Converts Hebrew text to visual text
1. String Functions ...
Function Description
hebrevc() Converts Hebrew text to visual text and new lines (n) into <br>
hex2bin() Converts a string of hexadecimal values to ASCII characters
html_entity_decode() Converts HTML entities to characters
htmlentities() Converts characters to HTML entities
htmlspecialchars_deode() Converts some predefined HTML entities to characters
htmlspecialchars() Converts some predefined characters to HTML entities
implode() Returns a string from the elements of an array
join() Alias of implode()
lcfirst() Converts the first character of a string to lowercase
levenshtein() Returns the Levenshtein distance between two strings
localeconv() Returns locale numeric and monetary formatting information
ltrim() Removes whitespace or other characters from the left side of a string
md5() Calculates the MD5 hash of a string
md5_file() Calculates the MD5 hash of a file
metaphone() Calculates the metaphone key of a string
money_format() Returns a string formatted as a currency string
nl_langinfo() Returns specific local information
1. String Functions ...
Function Description
print() Outputs one or more strings
printf() Outputs a formatted string
quoted_printable_decode() Converts a quoted-printable string to an 8-bit string
quoted_printable_encode() Converts an 8-bit string to a quoted printable string
quotemeta() Quotes meta characters
rtrim() Removes whitespace or other characters from the right side of a string
setlocale() Sets locale information
sha1() Calculates the SHA-1 hash of a string
sha1_file() Calculates the SHA-1 hash of a file
similar_text() Calculates the similarity between two strings
soundex() Calculates the soundex key of a string
sprintf() Writes a formatted string to a variable
sscanf() Parses input from a string according to a format
str_getcsv() Parses a CSV string into an array
str_ireplace() Replaces some characters in a string (case-insensitive)
str_pad() Pads a string to a new length
str_repeat() Repeats a string a specified number of times
1. String Functions ...
Function Description
str_word_count() Count the number of words in a string
strcasecmp() Compares two strings (case-insensitive)
strchr() Finds the first occurrence of a string inside another string (alias of strstr())
strcmp() Compares two strings (case-sensitive)
strcoll() Compares two strings (locale based string comparison)
strcspn() Returns the number of characters found in a string before any part of some specified characters are found
strip_tags() Strips HTML and PHP tags from a string
stripcslashes() Unquotes a string quoted with addcslashes()
stripslashes() Unquotes a string quoted with addslashes()
stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive)
stristr() Finds the first occurrence of a string inside another string (case-insensitive)
strlen() Returns the length of a string
strnatcasecmp() Compares two strings using a "natural order" algorithm (case-insensitive)
strnatcmp() Compares two strings using a "natural order" algorithm (case-sensitive)
strncasecmp() String comparison of the first n characters (case-insensitive)
strncmp() String comparison of the first n characters (case-sensitive)
strpbrk() Searches a string for any of a set of characters
1. String Functions ...
Function Description
strripos() Finds the position of the last occurrence of a string inside another string (case-insensitive)
strrpos() Finds the position of the last occurrence of a string inside another string (case-sensitive)
strspn() Returns the number of characters found in a string that contains only characters from a specified charlist
strstr() Finds the first occurrence of a string inside another string (case-sensitive)
strtok() Splits a string into smaller strings
strtolower() Converts a string to lowercase letters
strtoupper() Converts a string to uppercase letters
strtr() Translates certain characters in a string
substr() Returns a part of a string
substr_compare() Compares two strings from a specified start position (binary safe and optionally case-sensitive)
substr_count() Counts the number of times a substring occurs in a string
substr_replace() Replaces a part of a string with another string
trim() Removes whitespace or other characters from both sides of a string
ucfirst() Converts the first character of a string to uppercase
ucwords() Converts the first character of each word in a string to uppercase
vfprintf() Writes a formatted string to a specified output stream
vprintf() Outputs a formatted string
2. Constants
 Constants are like variables except that once they are defined
they cannot be changed or undefined.
 A constant is an identifier (name) for a simple value.
 The value cannot be changed during the script.
 A valid constant name starts with a letter or underscore (no
$ sign before the constant name).
Note: Unlike variables, constants are automatically global
across the entire script.
2. Set a PHP Constant
 To set a constant, use the define() function - it takes three
parameters:
 The first parameter defines the name of the constant,
 the second parameter defines the value of the constant, and
 the optional third parameter specifies whether the constant name
should be case-insensitive.
 Default is false.
Example 1:
<?php
// define a case-sensitive constant
define("GREETING", "Welcome to google.com!");
echo GREETING;
echo "<br>";
// will not output the value of the constant
echo greeting;
?>
2. Set a PHP Constant ...
Example 2: The example below creates a case-insensitive
constant with the value of "Welcome to google.com!":
<?php
// define a case-insensitive constant
define("GREETING", "Welcome to google.com!", true);
echo GREETING;
echo "<br>";
// will also output the value of the constant
echo greeting;
?>
3. Operators
3.1Arithmetic Operators
Example:
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th power
(Introduced in PHP 5.6)
<?php
$x=10;
$y=6;
echo ($x + $y); // outputs 16
echo ($x - $y); // outputs 4
echo ($x * $y); // outputs 60
echo ($x / $y); // outputs 1.6666666666667
echo ($x % $y); // outputs 4
?>
3.2 Assignment Operators
 The PHP assignment operators are used to write a value to a variable.
 The basic assignment operator in PHP is "=".
 It means that the left operand gets set to the value of the assignment expression on the right.
Example:
Assignment Same as... Description Assignment
x = y x = y The left operand gets set to the value of the expression on the right x = y
x += y x = x + y Addition x += y
x -= y x = x - y Subtraction x -= y
x *= y x = x * y Multiplication x *= y
x /= y x = x / y Division x /= y
x %= y x = x % y Modulus x %= y
<?php
$x=10;
echo $x; // outputs 10
$y=20;
$y += 100;
echo $y; // outputs 120
$z=50;
$z -= 25;
echo $z; // outputs 25
$i=5;
$i *= 6;
echo $i; // outputs 30
$j=10;
$j /= 5;
echo $j; // outputs 2
$k=15;
$k %= 4;
echo $k; // outputs 3
?>
3.3 String Operators
Example: <?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!
$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>
Operator Name Example Result
. Concatenation $txt1 = "Hello"
$txt2 = $txt1 . " world!"
Now $txt2 contains "Hello
world!"
.= Concatenation
assignment
$txt1 = "Hello"
$txt1 .= " world!"
Now $txt1 contains "Hello
world!
3.4 Increment / Decrement Operators
Example:
<?php
$x=10;
echo ++$x; // outputs 11
$y=10;
echo $y++; // outputs 10
$z=5;
echo --$z; // outputs 4
$i=5;
echo $i--; // outputs 5
?>
Operator Name Description
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one
3.5 Comparison Operators
Example:
<?php
$x=100;
$y="100";
var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";
$a=50;
$b=90;
var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?>
Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y True if $x is greater than $y
< Less than $x < $y True if $x is less than $y
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y
3.6 Logical Operators
Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
3.7 Array Operators
 The PHP array operators are used to compare arrays:
Example:
Operator Name Example Result
+ Union $x + $y Union of $x and $y (but duplicate keys are not overwritten)
== Equality $x == $y True if $x and $y have the same key/value pairs
=== Identity $x === $y True if $x and $y have the same key/value pairs in the same
order and of the same types
!= Inequality $x != $y True if $x is not equal to $y
<> Inequality $x <> $y True if $x is not equal to $y
!== Non-identity $x !== $y True if $x is not identical to $y
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
$z = $x + $y; // union of $x and $y
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

More Related Content

ODP
PHP Web Programming
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT II (7).pptx
PPTX
UNIT II (7).pptx
PPTX
Header file.pptx
PPT
Manipulating strings
PPTX
Day5 String python language for btech.pptx
PHP Web Programming
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT II (7).pptx
UNIT II (7).pptx
Header file.pptx
Manipulating strings
Day5 String python language for btech.pptx

Similar to Tokens in php (php: Hypertext Preprocessor).pptx (20)

PPTX
Php intro by sami kz
PPTX
String variable in php
PPT
Adv. python regular expression by Rj
PPTX
unit-5 String Math Date Time AI presentation
PDF
ANSI C REFERENCE CARD
PPTX
Unit_I-Introduction python programming (1).pptx
PPTX
Understanding the REST API of SharePoint 2013
PDF
Python Strings Methods
PPT
JavaScript Objects
PPT
Strings
PDF
Python regular expressions
PPS
String and string buffer
PDF
php_string.pdf
DOCX
Type header file in c++ and its function
PDF
Airoli_Grade 10_CompApp_PP000000000000000000000000000000000000000000T_Charact...
PDF
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
PPT
Strings in c
PPTX
Introduction to python programming ( part-3 )
PPTX
CH-3 FEATURES OF PYTHON, data types token
Php intro by sami kz
String variable in php
Adv. python regular expression by Rj
unit-5 String Math Date Time AI presentation
ANSI C REFERENCE CARD
Unit_I-Introduction python programming (1).pptx
Understanding the REST API of SharePoint 2013
Python Strings Methods
JavaScript Objects
Strings
Python regular expressions
String and string buffer
php_string.pdf
Type header file in c++ and its function
Airoli_Grade 10_CompApp_PP000000000000000000000000000000000000000000T_Charact...
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
Strings in c
Introduction to python programming ( part-3 )
CH-3 FEATURES OF PYTHON, data types token
Ad

More from BINJAD1 (20)

PPTX
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
PPTX
PostgreSQL (PostgreSQL is a versatile, open-source object-relational database...
PPTX
3D-Object Representation in Computer Graphics.pptx
PPTX
Visible Surface Detection Methods in Computer Graphics.pptx
PDF
Number Systems (These are ways of representing numbers using symbols and rule...
PDF
Computer Logical Organization(It refers to how its functional units are arran...
PDF
Logic Gates(Logic gates are fundamental building blocks of digital circuits).pdf
PDF
Digital Concepts (Digital electronics is a branch of electronics).pdf
PDF
sequential logic circuits- Latch & Flip Flop.pdf
PDF
Object Oriented Programming with Java Basic Syntax.pdf
PDF
AWT (Abstract Window Toolkit) Controls.pdf
PDF
Introduction to Microsoft Access (MS Access).pdf
PPTX
Database Administration (Database Administrator (DBA) is a professional respo...
PPTX
Database Administration (Database Administrator (DBA) is a professional respo...
PPTX
Database (DB- A database is an electronically stored, systematic collection o...
PPTX
Pixel- A pixel, short for "picture element.pptx
PPT
Introduction to Computer Graphics or CG.ppt
PPT
2D-Transformations-Transformations are the operations applied to geometrical ...
PPTX
2D Viewing- the window by setting a two-dimensional viewing co-ordinate syst...
PPTX
The internet and WWW-he terms World Wide Web (WWW) and the Internet
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
PostgreSQL (PostgreSQL is a versatile, open-source object-relational database...
3D-Object Representation in Computer Graphics.pptx
Visible Surface Detection Methods in Computer Graphics.pptx
Number Systems (These are ways of representing numbers using symbols and rule...
Computer Logical Organization(It refers to how its functional units are arran...
Logic Gates(Logic gates are fundamental building blocks of digital circuits).pdf
Digital Concepts (Digital electronics is a branch of electronics).pdf
sequential logic circuits- Latch & Flip Flop.pdf
Object Oriented Programming with Java Basic Syntax.pdf
AWT (Abstract Window Toolkit) Controls.pdf
Introduction to Microsoft Access (MS Access).pdf
Database Administration (Database Administrator (DBA) is a professional respo...
Database Administration (Database Administrator (DBA) is a professional respo...
Database (DB- A database is an electronically stored, systematic collection o...
Pixel- A pixel, short for "picture element.pptx
Introduction to Computer Graphics or CG.ppt
2D-Transformations-Transformations are the operations applied to geometrical ...
2D Viewing- the window by setting a two-dimensional viewing co-ordinate syst...
The internet and WWW-he terms World Wide Web (WWW) and the Internet
Ad

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
sap open course for s4hana steps from ECC to s4
Building Integrated photovoltaic BIPV_UPV.pdf
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx

Tokens in php (php: Hypertext Preprocessor).pptx

  • 2. 1. String Functions  A string is a sequence of characters, like "Hello world!". 1) The PHP strlen() function:  The strlen() function returns the length of a string, in characters.  The example below returns the length of the string "Hello world!": Example: Tip: strlen() is often used in loops or other functions, when it is important to know when a string ends.  i.e. in a loop, we might want to stop the loop after the last character in a string. <?php echo strlen("Hello world!"); ?>
  • 3. 1. String Functions ... 2) The PHP strpos() function  The strpos() function is used to search for a specified character or text within a string.  If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE.  The example below searches for the text "world" in the string "Hello world!": Example: Tip: The position of the string "world" in the example above is 6.  The reason that it is 6 (and not 7), is that the first character position in the string is 0, and not 1. <?php echo strpos("Hello world!","world"); ?>
  • 4. 1. String Functions ... Function Description addcslashes() Returns a string with backslashes in front of the specified characters addslashes() Returns a string with backslashes in front of predefined characters bin2hex() Converts a string of ASCII characters to hexadecimal values chop() Removes whitespace or other characters from the right end of a string chr() Returns a character from a specified ASCII value chunk_split() Splits a string into a series of smaller parts convert_cyr_string() Converts a string from one Cyrillic character-set to another convert_uudecode() Decodes a uuencoded string convert_uuencode() Encodes a string using the uuencode algorithm count_chars() Returns information about characters used in a string crc32() Calculates a 32-bit CRC for a string crypt() One-way string encryption (hashing) echo() Outputs one or more strings explode() Breaks a string into an array fprintf() Writes a formatted string to a specified output stream get_html_translation_table() Returns the translation table used by htmlspecialchars() and htmlentities() hebrev() Converts Hebrew text to visual text
  • 5. 1. String Functions ... Function Description hebrevc() Converts Hebrew text to visual text and new lines (n) into <br> hex2bin() Converts a string of hexadecimal values to ASCII characters html_entity_decode() Converts HTML entities to characters htmlentities() Converts characters to HTML entities htmlspecialchars_deode() Converts some predefined HTML entities to characters htmlspecialchars() Converts some predefined characters to HTML entities implode() Returns a string from the elements of an array join() Alias of implode() lcfirst() Converts the first character of a string to lowercase levenshtein() Returns the Levenshtein distance between two strings localeconv() Returns locale numeric and monetary formatting information ltrim() Removes whitespace or other characters from the left side of a string md5() Calculates the MD5 hash of a string md5_file() Calculates the MD5 hash of a file metaphone() Calculates the metaphone key of a string money_format() Returns a string formatted as a currency string nl_langinfo() Returns specific local information
  • 6. 1. String Functions ... Function Description print() Outputs one or more strings printf() Outputs a formatted string quoted_printable_decode() Converts a quoted-printable string to an 8-bit string quoted_printable_encode() Converts an 8-bit string to a quoted printable string quotemeta() Quotes meta characters rtrim() Removes whitespace or other characters from the right side of a string setlocale() Sets locale information sha1() Calculates the SHA-1 hash of a string sha1_file() Calculates the SHA-1 hash of a file similar_text() Calculates the similarity between two strings soundex() Calculates the soundex key of a string sprintf() Writes a formatted string to a variable sscanf() Parses input from a string according to a format str_getcsv() Parses a CSV string into an array str_ireplace() Replaces some characters in a string (case-insensitive) str_pad() Pads a string to a new length str_repeat() Repeats a string a specified number of times
  • 7. 1. String Functions ... Function Description str_word_count() Count the number of words in a string strcasecmp() Compares two strings (case-insensitive) strchr() Finds the first occurrence of a string inside another string (alias of strstr()) strcmp() Compares two strings (case-sensitive) strcoll() Compares two strings (locale based string comparison) strcspn() Returns the number of characters found in a string before any part of some specified characters are found strip_tags() Strips HTML and PHP tags from a string stripcslashes() Unquotes a string quoted with addcslashes() stripslashes() Unquotes a string quoted with addslashes() stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive) stristr() Finds the first occurrence of a string inside another string (case-insensitive) strlen() Returns the length of a string strnatcasecmp() Compares two strings using a "natural order" algorithm (case-insensitive) strnatcmp() Compares two strings using a "natural order" algorithm (case-sensitive) strncasecmp() String comparison of the first n characters (case-insensitive) strncmp() String comparison of the first n characters (case-sensitive) strpbrk() Searches a string for any of a set of characters
  • 8. 1. String Functions ... Function Description strripos() Finds the position of the last occurrence of a string inside another string (case-insensitive) strrpos() Finds the position of the last occurrence of a string inside another string (case-sensitive) strspn() Returns the number of characters found in a string that contains only characters from a specified charlist strstr() Finds the first occurrence of a string inside another string (case-sensitive) strtok() Splits a string into smaller strings strtolower() Converts a string to lowercase letters strtoupper() Converts a string to uppercase letters strtr() Translates certain characters in a string substr() Returns a part of a string substr_compare() Compares two strings from a specified start position (binary safe and optionally case-sensitive) substr_count() Counts the number of times a substring occurs in a string substr_replace() Replaces a part of a string with another string trim() Removes whitespace or other characters from both sides of a string ucfirst() Converts the first character of a string to uppercase ucwords() Converts the first character of each word in a string to uppercase vfprintf() Writes a formatted string to a specified output stream vprintf() Outputs a formatted string
  • 9. 2. Constants  Constants are like variables except that once they are defined they cannot be changed or undefined.  A constant is an identifier (name) for a simple value.  The value cannot be changed during the script.  A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.
  • 10. 2. Set a PHP Constant  To set a constant, use the define() function - it takes three parameters:  The first parameter defines the name of the constant,  the second parameter defines the value of the constant, and  the optional third parameter specifies whether the constant name should be case-insensitive.  Default is false. Example 1: <?php // define a case-sensitive constant define("GREETING", "Welcome to google.com!"); echo GREETING; echo "<br>"; // will not output the value of the constant echo greeting; ?>
  • 11. 2. Set a PHP Constant ... Example 2: The example below creates a case-insensitive constant with the value of "Welcome to google.com!": <?php // define a case-insensitive constant define("GREETING", "Welcome to google.com!", true); echo GREETING; echo "<br>"; // will also output the value of the constant echo greeting; ?>
  • 12. 3. Operators 3.1Arithmetic Operators Example: Operator Name Example Result + Addition $x + $y Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y ** Exponentiation $x ** $y Result of raising $x to the $y'th power (Introduced in PHP 5.6) <?php $x=10; $y=6; echo ($x + $y); // outputs 16 echo ($x - $y); // outputs 4 echo ($x * $y); // outputs 60 echo ($x / $y); // outputs 1.6666666666667 echo ($x % $y); // outputs 4 ?>
  • 13. 3.2 Assignment Operators  The PHP assignment operators are used to write a value to a variable.  The basic assignment operator in PHP is "=".  It means that the left operand gets set to the value of the assignment expression on the right. Example: Assignment Same as... Description Assignment x = y x = y The left operand gets set to the value of the expression on the right x = y x += y x = x + y Addition x += y x -= y x = x - y Subtraction x -= y x *= y x = x * y Multiplication x *= y x /= y x = x / y Division x /= y x %= y x = x % y Modulus x %= y <?php $x=10; echo $x; // outputs 10 $y=20; $y += 100; echo $y; // outputs 120 $z=50; $z -= 25; echo $z; // outputs 25 $i=5; $i *= 6; echo $i; // outputs 30 $j=10; $j /= 5; echo $j; // outputs 2 $k=15; $k %= 4; echo $k; // outputs 3 ?>
  • 14. 3.3 String Operators Example: <?php $a = "Hello"; $b = $a . " world!"; echo $b; // outputs Hello world! $x="Hello"; $x .= " world!"; echo $x; // outputs Hello world! ?> Operator Name Example Result . Concatenation $txt1 = "Hello" $txt2 = $txt1 . " world!" Now $txt2 contains "Hello world!" .= Concatenation assignment $txt1 = "Hello" $txt1 .= " world!" Now $txt1 contains "Hello world!
  • 15. 3.4 Increment / Decrement Operators Example: <?php $x=10; echo ++$x; // outputs 11 $y=10; echo $y++; // outputs 10 $z=5; echo --$z; // outputs 4 $i=5; echo $i--; // outputs 5 ?> Operator Name Description ++$x Pre-increment Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one
  • 16. 3.5 Comparison Operators Example: <?php $x=100; $y="100"; var_dump($x == $y); echo "<br>"; var_dump($x === $y); echo "<br>"; var_dump($x != $y); echo "<br>"; var_dump($x !== $y); echo "<br>"; $a=50; $b=90; var_dump($a > $b); echo "<br>"; var_dump($a < $b); ?> Operator Name Example Result == Equal $x == $y True if $x is equal to $y === Identical $x === $y True if $x is equal to $y, and they are of the same type != Not equal $x != $y True if $x is not equal to $y <> Not equal $x <> $y True if $x is not equal to $y !== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type > Greater than $x > $y True if $x is greater than $y < Less than $x < $y True if $x is less than $y >= Greater than or equal to $x >= $y True if $x is greater than or equal to $y <= Less than or equal to $x <= $y True if $x is less than or equal to $y
  • 17. 3.6 Logical Operators Operator Name Example Result and And $x and $y True if both $x and $y are true or Or $x or $y True if either $x or $y is true xor Xor $x xor $y True if either $x or $y is true, but not both && And $x && $y True if both $x and $y are true || Or $x || $y True if either $x or $y is true ! Not !$x True if $x is not true
  • 18. 3.7 Array Operators  The PHP array operators are used to compare arrays: Example: Operator Name Example Result + Union $x + $y Union of $x and $y (but duplicate keys are not overwritten) == Equality $x == $y True if $x and $y have the same key/value pairs === Identity $x === $y True if $x and $y have the same key/value pairs in the same order and of the same types != Inequality $x != $y True if $x is not equal to $y <> Inequality $x <> $y True if $x is not equal to $y !== Non-identity $x !== $y True if $x is not identical to $y <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // union of $x and $y var_dump($z); var_dump($x == $y); var_dump($x === $y); var_dump($x != $y); var_dump($x <> $y); var_dump($x !== $y); ?>