0% found this document useful (0 votes)
39 views

PHP Unit-I

PHP is a server-side scripting language designed for web development that allows code to be embedded within HTML pages. When a page with PHP code is visited, the PHP code is executed on the server and generates the HTML or other output that is sent to the browser. PHP code is interpreted at the web server before the page is delivered to the user.

Uploaded by

Dhruv Rawal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

PHP Unit-I

PHP is a server-side scripting language designed for web development that allows code to be embedded within HTML pages. When a page with PHP code is visited, the PHP code is executed on the server and generates the HTML or other output that is sent to the browser. PHP code is interpreted at the web server before the page is delivered to the user.

Uploaded by

Dhruv Rawal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Introduction to PHP

“PHP is a server-side scripting language


designed specifically for the Web. Within an
HTML page, you can embed PHP code that will
be executed each time the page is visited. Your
PHP code is interpreted at the Web server and
generates HTML or other output that the visitor
will see” (“PHP and MySQL Web Development”,
Luke Welling and Laura Thomson, SAMS)
Science Calculations

System

System

http://en.wikipedia.org/wiki/History_of_programming_languages
Client/Server on the WWW
● Standard web sites operate on a request/response basis
● A user requests a resource E.g. HTML document
● Server responds by delivering the document to the client
● The client processes the document and displays it to
user
● Ordinarily the client/response model merely serves a text
file over the HTTP protocol to the user agent application,
which then renders the output appropriately.
● The protocol is inherently stateless (meaning that each
request/response is dealt with individually).
Server Side Programming
● Provides web site developers to utilise resources on the
web server
● Non-public resources do not require direct access from
the clients
● Allows web sites to be client agnostic (unless JavaScript
is used also)
● User/Client authentication is needed.
● Most server side programming script is embedded within
markup
● although does not have to be, sometimes better not to.
Request to a Static Site
Request to a Dynamic Site
● The server must respond dynamically if it needs to provide
different client-side code depending on the situation
● Date and time
● Specifics of the user’s request
● Database contents – forms and authentication
PHP - What is it / does it do?
● PHP is a recursive acronym for “PHP: Hypertext
Preprocessor”
● Server-side scripting language that may be embedded into
HTML
● hence well suited for web development
● Programming language that is interpreted and executed on the server
● Execution is done before delivering content to the client
● Ultimate goal is to get PHP files to generate client-side code
● must end up with HTML, CSS, JavaScript, other client-side code!
● Executes entirely on the server, requiring no specific features
from the client
● It is a widely-used open source general-purpose scripting
language used to create web applications in combination with
a web server, such as Apache
PHP - What is it / does it do?
● Contains a vast library of functionality that programmers
can harness.
● PHP can also be used to create command-line scripts
akin to Perl or shell scripts
● but such use is much less common than PHP’s use as a web
language.
● Strictly speaking, PHP has nothing to do with layout,
events, on-the-fly Document Object Model (DOM)
manipulation, or really anything about the look and feel
of a web page.
● In fact, most of what PHP does is invisible to the end
user.
● Someone looking at a PHP page will not necessarily be able to
tell that it was not written purely in Hypertext Markup Language
(HTML), because the result of PHP is usually HTML.
PHP - What is it / does it do?
● Static resources such as regular HTML are simply output to
the client from the server
● Dynamic resources such as PHP scripts are processed on the
server prior to being output to the client
● PHP has the capability of connecting to many database
systems making the entire process transparent to the client

Web Page Request Load PHP File


PHP
Engine –
Run Script
HTML Response PHP Results
PHP - What is it / does it do?
Server
side
Script
PHP
engine
interp-
etation

Client
side
HTML
page
How PHP generates HTML/JS Web
pages
Client
Browser

1 4 PHP
3
module
Apache
2

1: Client from browser send HTTP request (with POST/GET variables)


2: Apache recognizes that a PHP script is requested and sends the
request to PHP module
3: PHP interpreter executes PHP script, collects script output and
sends it back
4: Apache replies to client using the PHP script output as HTML output
PHP Summary
● PHP: PHP Hypertext Pre-processor
● Interpreted and executed by the server on page request
● Returns simple output to the client
● Provides a tremendous amount of functionality to
programmers
● Can connect transparently to many database systems
● Therefore we cannot save them anywhere and view
them, as with HTML files
● Must save .php files in subdirectory of web server
● /var/www/ on many Linux configurations
● www directory of your user directory
PHP Brief History
● 1994: Created by Rasmis Lesdorf, software engineer
(part of Apache Team)
● 1995: Called Personal Home Page Tool, then released
as version 2 with name PHP/FI (Form Interpreter, to
analyze SQL queries)
● Half 1997: used by 50,000 web sites
● October 1998: used by 100,000 websites
● End 1999: used by 1,000,000 websites
Features of PHP
● Open-source, so PHP is free to download and use.
● Can download from the official PHP resource: www.php.net
● Compatible with almost all servers used today (Apache,
IIS, etc.)
● Easy to use ( C-like and Perl-like syntax)
● Stable and fast
● Multiplatform (Windows, Linux, Unix, Mac, etc.)
● Many databases support (MySQL, Informix, Oracle,
Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
● Many common built-in libraries
● Pre-installed in Linux distributions
Alternatives to PHP
● Practical extraction and Report Language (Perl)
● Active Server Pages (ASP)
● Java server pages (JSP)
● Ruby
What Is MySQL?
● MySQL is an open source, SQL relational database
management system (RDBMS) that is free for many uses.
● MySQL is ideal for both small and large applications
● Databases are generally useful, perhaps the most
consistently useful family of software products (the “killer
product”) in modern computing.
● Like many competing products, its both free and
commercial.
What Is MySQL?
● Its acceptance was aided in part by the wide variety of
other technologies such as PHP, Perl, Python, and the like
that have encouraged its use through stable,
well-documented modules and extensions.
● MySQL supports standard SQL
● MySQL compiles on a number of platforms
Deciding on a Web Application
Platform
● There are many platforms upon which web applications can
be built.

Fast feature
Cost Many extensions
development

Ease of Use Stability Not proprietary

Cross-platform Strong user


HTML-embeddedness
compatibility communities
Getting Started with PHP:
Installing PHP
● PHP runs on various platforms, including Linux, various
Unix flavors, Microsoft Windows, and Mac OS X. Linux is
the most popular platform for PHP, and when combined
with the Apache web server, and MySQL forms the
acronym LAMP.
● For Windows you can install XAMPP or WAMP.
● We also need Editor. We can use Notepad++, Brackets,
Eclipse, NetBeans.
● Your HTML Is Already PHP-Compliant!
● PHP is generally embedded within HTML.
● Anything compatible with HTML on the client side is also
compatible with PHP.
Getting Started with PHP:
Installing PHP
● Escaping from HTML
● How does the PHP parser recognize PHP code inside your HTML
document?
● The answer is that you tell the program when to spring into action
by using special PHP tags at the beginning and end of each PHP
section. This process is called escaping from HTML or escaping
into PHP.
● (What is escape characters? Don’t confuse here)
● Everything within these tags is understood by the PHP parser to
be PHP code. Everything outside of these tags does not concern
the server and will simply be passed along and left for the client to
sort out whether it’s HTML or JavaScript or something else.
● There are several styles of PHP, but it’s best to stick with the
tried-and-true tags that will always work no matter which version of
PHP you’re using
Canonical PHP tags
● The most universally effective PHP tag style is:
<?php
?>
● If you use this style, you can be positive that your tags will
always be correctly interpreted.
● These symbols (<?php and ?>)are called PHP tags.
PHP from command Line
● You can run PHP from
the command line - the
output simply comes out <?php
on the terminal. echo("Hello World!");
● It does not have to be echo("\n");
part of a ?>
request-response cycle.
A simple PHP web example
<html>
<head>
<title>A simple html demo</title>
</head>
<body>
<h1>HTML & php embedding</h1>
<?php
echo '<p>A simple phpdemo</p>';
?>
</body>
</html>
HTML page rendered by PHP
Output:

<html>
<head>
<title>A simple html demo</title>
</head>
<body>
<h1>HTML & php embedding</h1>
<p>A simple phpdemo</p>;
</body>
</html>
Understanding PHP
● This example illustrates the concept of server-side scripting in
a nutshell.
● The PHP has been interpreted and executed on the web
server, as distinct from Java Script and other client-side
technologies interpreted and executed within a web browser
on a user’s machine.
● The code that you now have in this file consists of four types
of text:
● HTML
● PHP tags
● PHP statements
● White space
● Most of the lines in the example are just plain HTML.
Comments in PHP
● A comment is the portion of a program
that exists only for the human reader.
● The very first thing that a program
executor does with program code is to
strip out the comments, so they cannot
have any effect on what the program
does.
● Comments are invaluable in helping the
next person who reads your code figure
out what you were thinking when you
wrote it, even when that person is
yourself a week from now.
● In PHP, we use // to make a single-line
comment or /* and */ to make a large
comment block.
Jumping in and out of PHP mode
● At any given moment in a PHP script, you are either in PHP
mode or you’re out of it in HTML.
● There’s no middle ground. Anything within the PHP tags is
PHP; everything outside is plain HTML, as far as the server is
concerned.
● <?php $id = 1; ?>
● <FORM METHOD=”POST” ACTION=”registration.php”“>
<P>First name:<INPUT TYPE=”TEXT” NAME=”firstname” SIZE=”20”>
<P>Last name:<INPUT TYPE=”TEXT” NAME=”lastname” SIZE=”20”>
<P>Rank:<INPUT TYPE=”TEXT” NAME=”rank” SIZE=”10”>
<INPUT TYPE=”HIDDEN” NAME=”serial number” VALUE=” <?php
echo $id; ?> ”>
<INPUT TYPE=”submit”SUBMIT” VALUE=”INPUT”“> </FORM>
PHP Language
Basics
Unit-I
PHP Language Basics
● Look at the building blocks of the PHP
language
● Syntax and structure
● Variables, constants and operators
● Data types and conversions
● Decision making IF and switch
● Interacting with the client application (HTML
forms)
PHP - Syntax and Structure
● PHP is similar to C
● All scripts start with <?php and with with ?>
● Line separator: ; (semi-colon)
● Code block: { //code here } (brace brackets)
● White space is generally ignored (not in strings)
● Comments are created using:
● // or # single line comments
● /* Multiple line block comment */
● Precedence
● Enforced using parentheses
● E.g. $sum = 5 + 3 * 6; // would equal 23
● $sum = (5 + 3) * 6; // would equal 48
PHP Variables
● The main way to store information in the middle of a PHP
program is by using a variable
● a way to name and hang on to any value that you want to use later.
● 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.
PHP Variables
● Variables used before they are assigned have default values.
● After the initial $, variable names must be composed of letters
(uppercase or lowercase), digits (0–9), and underscore
characters (_).
● Furthermore, the first character after the $ may not be a number.
● Variable names are case-sensitive, i.e. $author and $Author are
different
● A variable name should not contain spaces. If a variable name is
more than one word, it should be separated with an underscore
($my_string) or with capitalization ($myString)

$abc = 12; abc = 12;


$total = 0; $2php = 0;
$largest_so_far = 0; $bad-punc = 0;
PHP - Example Script
<?php
$name = “Mark Zuckerberg”;
$msg = “Hello world!”;
echo $name . “ says ” . $msg;
?>

$x = 2; $x = 2;
$y = x + 5; y = $x + 5;
print $y; print $x;

Output: 5 Parse error


PHP Variables
● Declaring variables
● In languages such as C, C++, and Java, the programmer
must declare the name and type of any variable before
making use of it.
● However in PHP, because 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:
PHP Variables
● $pi = 3 + 0.14159; // approximately
● Note that what is assigned is the result of evaluating the
expression, not the expression itself.
● Reassigning variables:
● There is no interesting distinction in PHP between assigning
a variable for the first time and changing its value later.
● This is true even if the assigned values are of different types.
● Example, the following is perfectly legal:
● $my_num_var = “This should be a number – hope it’s reassigned”;
● $my_num_var = 5;
● If the second statement immediately follows the first one, the
first statement has essentially no effect.
PHP Variables
● Unassigned variables:
● Many programming languages will object if you try to use a
variable before it is assigned; others will let you use it, but if
you do you may find yourself reading the random contents
of some area of memory.
● In PHP, the default error-reporting setting allows you to use
unassigned variables without errors, and PHP ensures that
they have reasonable default values.
● PHP provides a function called isset() that tests a variable
to see whether it has been assigned a value or not.
● the function unset() will restore a variable to an unassigned
state
PHP Variables
● Default values:
● Variables in PHP do not have intrinsic types — a variable does
not know in advance whether it will be used to store a number or
a string of characters.
● So how does it know what type of default value to have when it
hasn’t yet been assigned?
● The answer is that, just as with assigned variables, the type of a
variable is interpreted depending on the context in which it is
used.
● 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 (the
string that is zero characters long).
Output
Example:
● echo is a language construct -
can be treated like a function <?php
with one parameter. $x = "15" + 27;
● When used without echo $x;
parentheses, it accepts echo("\n");
multiple parameters. echo $x, "\n";
print $x;
● print is a function - only one print "\n";
parameter, but parentheses print($x);
are optional so it can look like print("\n");
a language construct. ?>
Printing and Output
● PHP also offers printf() and sprintf(), which are modeled on C
functions of the same name.
● Example:
<?php
$value = 3.14159;
printf(“%f,%10f,%-010f,%2.2f\n”, $value, $value, $value,
$value);
?>
● gives us:
3.141590, 3.141590,3.141590000000000, 3.14
● The only difference between printf() and sprintf() is that printf()
sends the resulting string directly to output, whereas sprintf()
returns the result string as its value.
PHP - Constants
● Constants are special variables that cannot be changed
● Use them for named items that will not change
● PHP offers constants, which have a single value throughout
their lifetime.
● Constants do not have a $ before their names, and by
convention the names of constants usually are in uppercase
letters.
● Constants can contain only scalar values (numbers & string)
● Constants have global scope, so they are accessible
everywhere in your scripts after they have been defined —
even inside functions.
PHP - Constants
● It’s also possible to create your own constants using the
define() function. The code:
define(‘MILESTOKM’, 1.6);
$km = 5 * MILESTOKM;
● would cause MILESTOKM to evaluate to 1.6 everywhere
it appears in your code.
● Hence the value of variable “km” will be 8.0
● There is no way to change this assignment after it has been
made.
PHP Data Types: Don’t Worry,
Be Happy
● All programming languages have some kind of type
system, which specifies the different kinds of values that
can appear in programs.
● 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 for you.
● No variable type declarations:
● The type of a variable does not need to be declared in
advance.
PHP Data Types: Don’t Worry,
Be Happy
● PHP is not strictly typed
● Different to JAVA where all variables are declared
● A data type is either text or numeric
● PHP decides what type a variable is
● PHP can use variables in an appropriate way automatically
● Example:
$vat_rate = 0.175; /* VAT Rate is numeric */
echo $vat_rate * 100 . “%”; //outputs “17.5%”
● $vat_rate is converted to a string for the purpose of the
echo statement
● Object, Array and unknown also exist as types.
PHP Data Types: Don’t Worry,
Be Happy
● Automatic type conversion:
● PHP does a good job of automatically converting types
when necessary.
● Like most other modern programming languages, PHP
will do the right thing when, for example, doing math with
mixed numerical types. The result of the expression:
$pi = 3 + 0.14159;
● is a floating-point (double) number,
● with the integer 3 implicitly converted into floating point before the
addition is performed.
PHP Data Types: Don’t Worry,
Be Happy
● Types assigned by context:
● PHP goes further than most languages in performing
automatic type conversions. Consider:
$sub = substr(12345, 2, 2);
print(“sub is $sub<BR>”);
● The substr function is designed to take
● a string of characters as its first input and
● return a substring of that string, with the start point and length
determined by the next two inputs to the function.
● Instead of handing the function a character string,
however, we gave it the integer 12345
● Still it will work and not give an error
Data-type Summary
● PHP has a total of eight types: integers, doubles, Booleans, strings,
arrays, objects, NULL, and resources.
● 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 Data-types
● Many programming languages have several different sizes of numerical
types, with the larger ones allowing a greater range of values, but also
taking up more room in memory.
● For example, the C language has a short type (for relatively small
integers), a long type (for possibly larger integers), and an int type
(which might be intermediate, but in practice is sometimes identical
either to the short or long type). It also has floating-point types, which
vary in their precision.
● This kind of typing choice made sense in an era when tradeoffs between
memory use and functionality were often agonizing.
● The PHP having only two numerical types, corresponding to the largest
of the integral and floating-point types in C.
The Simple Data-types
● 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;
● Read formats:
● Integers can actually be read in three formats, which correspond to
bases: decimal (base 10), octal (base 8), and hexadecimal (base 16).
● Decimal format is the default, octal integers are specified with a leading
0, and hexadecimals have a leading 0x.
● Any of the formats can be preceded by a - sign to make the integer
negative.
The Simple Data-types
● For example:
● $integerB10 = 1000;
● $integerB8 = -01000;
● $integerB16 = 0x1000;
● Integer Range:
● PHP integers correspond to the C long type
● The largest integer is 2^31 – 1 (or 2,147,483,647), and the smallest
(most negative) integer is –(2^31 – 1) (or –2,147,483,647).
● The PHP constant PHP_INT_MAX will tell you the maximum integer for
your implementation
The Simple Data-types
● Doubles: Doubles are floating-point numbers, such as:
● $first_double = 123.456;
● Read formats: The typical read format for doubles is -X.Y, where the -
optionally specifies a negative number, and both X and Y are sequences
of digits between 0 and 9.
● The X part may be omitted if the number is between –1.0 and 1.0, and the
Y part can also be omitted.
● Leading or trailing zeros have no effect.
● All the following are legal doubles:
● $small_positive = 0.12345;
● $small_negative = -.12345;
● $even_double = 2.00000;
● $still_double = 2.;
● Doubles can be specified in scientific notation, by adding the letter e and a
desired integral power of 10 to the end of the previous format — for
example, 2.2e3 would correspond to 2.2 x 10^3
The Simple Data-types
● Booleans: Booleans are true-or-false values, which are used in control
constructs like the testing portion of an if statement.
● Boolean truth values can be combined using logical operators to make
more complicated Boolean expressions.
● PHP provides a couple of constants especially for use as Booleans: TRUE
and FALSE, which can be used like this:
● Example:
if (TRUE)
print(“This will always print<BR>”);
else
print(“This will never print<BR>”);
The Simple Data-types
● Interpreting other types as Booleans: Here are the rules for determine the
“truth” of any value not already of the Boolean type:
● If the value is a number, if the number is zero, it is false and true if the
value is non-zero.
● For a string value,if the string is empty (has zero characters) it is false,
otherwise it is true.
● Values of type NULL are always false.
● For a compound type (an array or an object), if it contains no other values
it is false, otherwise it is true.
● For an object, containing a value means having a member variable that
has been assigned a value.
● Valid resources are true (although some functions that return resources
when they are successful will return FALSE when unsuccessful).
The Simple Data-types
● NULL: The NULL type 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 special constant NULL is capitalized by convention, but actually it is
case insensitive; you could just as well have typed: $my_var = null;
● NULL represents the lack of a value. A variable that has been assigned
the value NULL is nearly indistinguishable from a variable that has not
been set at all. In particular, a variable that has been assigned NULL has
the following properties:
● It evaluates to FALSE in a Boolean context.
● It returns FALSE when tested with IsSet(). (No other type has this property.)
● PHP will not print warnings if you pass the variable to functions and back
again, whereas passing a variable that has never been set will sometimes
produce warnings.
The Simple Data-types
● Strings: Strings are character sequences, as in the following:
● $string_1 = “This is a string in double quotes.”;
● $string_2 = ‘This is a somewhat longer, singly quoted string’;
● $string_39 = “This string has thirty-nine characters.”;
● $string_0 = “”; // a string with zero characters
● Strings can be enclosed in either single or double quotation marks, with
different behavior at read time.
● Singly quoted strings are treated almost like literally, whereas doubly
quoted strings replace variables with their values as well as specially
interpreting certain character sequences.
● Singly quoted strings:Except for a couple of specially interpreted
character sequences, singly quoted strings read in and store their
characters literally.
The Simple Data-types
● Consider the following code:
$literally = ‘My $variable will not print!\\n’;
print($literally);
● produces the browser output:
My $variable will not print!\n
● Doubly quoted strings: Strings that are delimited by double quotes (as in
“this”) are preprocessed in both the following two ways by PHP:
● Certain character sequences beginning with backslash (\) are
replaced with special characters.
● Variable names (starting with $) are replaced with string
representations of their values
The Simple Data-types
● The escape-sequence replacements are:
● \n is replaced by the newline character
● \r is replaced by the carriage-return character
● \t is replaced by the tab character
● \$ is replaced by the dollar sign itself ($)
● \“ is replaced by one double quotation mark (“)
● \\ is replaced by a single backslash (\)
Variable scope
● Scope is the technical term for the rules about when a name (for, say,
a variable or function) has the same meaning in two different places
and in what situations two names spelled exactly the same way can
actually refer to different things.
● Any PHP variable not inside a function has global scope and extends
throughout a given “thread” of execution.
● In other words, if you assign a variable near the top of a PHP file, the
variable name has the same meaning for the rest of the file; and if it is
not reassigned, it will have the same value as the rest of your code
executes (except inside the body of functions and classes).
● The assignment of a variable will not affect the value of variables with
the same name in other PHP files or even in repeated uses of the
same file.
PHP Arithmetic Operators
Operato Description Example Result
r
+ 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
PHP Assignment Operators
Operator Description Example Is The Same As

= Assign $x = $y $x = $y
+= Add and assign $x += $y $x = $x + $y
-= Subtract and assign $x -= $y $x = $x - $y
*= Multiply and assign $x *= $y $x = $x * $y
/= Divide and assign quotient $x /= $y $x = $x / $y
%= Divide and assign modulus $x %= $y $x = $x % $y
PHP Comparison Operators
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
< Less than $x < $y True if $x is less than $y
> Greater than $x > $y True if $x is greater than $y
>= Greater than or $x >= $y True if $x is greater than or equal to $y
equal to
<= Less than or equal $x <= $y True if $x is less than or equal to $y
to
Incrementing and
Decrementing Operators

Operator Name Effect Operator


++$x Pre-increment Increments $x by one, then returns $x ++$x

$x++ Post-increment Returns $x, then increments $x by one $x++

--$x Pre-decrement Decrements $x by one, then returns $x --$x

$x-- Post-decrement Returns $x, then decrements $x by one $x--


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

You might also like