PHP (Hypertext Processor) is a general-purpose scripting language and interpreter that is freely available and widely used for web development. The language is used primarily for server-side scripting, although it can also be used for command-line scripting and, to a limited degree, desktop applications.
PHP is a programming language used to script websites that are dynamic and interactive. You'll find it in various types of web applications, from e-commerce websites to CRM systems like HubSpot and Salesforce. The term PHP stands for PHP Hypertext Preprocessor.
The full form of PHP is Hypertext Preprocessor. It was abbreviated previously as Personal Home Page. It is a programming language widely used to build web applications or websites. It is the server-side scripting language encoded with HTML to develop Dynamic website, Static website or Web applications.
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types: Scalar Types (predefined) Compound Types (user-defined) Special Types.
2. ■ Origins and uses of PHP
■ Overview of PHP
■ General Syntactic Characteristics
■ Primitives
■ Operations and Expressions
■ Output
■ Control Statements
3. Origins and uses of PHP
• PHP was developed by Rasmus Lerdorf a member
of the Apache Group.
• In 1995 Lerdorf originally named it Personal Home
Page (PHP) but currently is known asHypertext
Preprocessor.
• PHP is a server-side, scripting language executed on
the server.
• PHP is a widely used, open-source scripting language .
4. Overview of PHP
PHP processor has two modes of operations:
1. Copy mode :
On the server side when the PHP processor finds XHTML code in the input
file, it simply copies it to the output file.
2. Interpret mode:
On the server side when it encounters a PHP script in the input file, it
interprets it and sendsany output of the script to the output file.
5. General Syntactic Characteristics
• PHP scripts can be embedded within the XHTML document by enclosing
it between the <?php and ?> tags.
• Example:
<!DOCTYPE html>
<html>
<body>
<? php
// single-line comment
# single-line comment
/*
multiple-lines
comment block
*/
echo “Hello World”;
?>
</body>
</html>
6. PHP allows comments to be specified in three different ways:
• Single-line comments can be specified either with # or with //.
• Multiple-line comments are delimited with /* and */ , as in many other
programming languages.
• PHP statements are terminated with semicolons(;).
• All variables in PHP begin with dollar sign($) followed by the name of the
variable. The variable names are case sensitive, $age and $Age are not same.
• Variables are not declared in PHP; they have a value and the type of the value.
7. • A variable name cannot start with a number. A variable name can only contain
alpha-numeric characters and underscores (A-z, 0-9, and _ ).
• A variable name must start with a letter or the underscore character.
8. • Reserved words of PHP
• Although variable names in PHP are case sensitive, neither reserved words nor
function names are
• For example,
there is no difference between while, WHILE, While, wHile.
9. Primitives, operations and expressions
• PHP has four scalar data types:
-Boolean
- Integer
-Double and
-String
• PHP has two compound data types:
- Array
- Object
• PHP has two special types:
-Resource
-NULL
10. • Integer type:
-PHP integer is corresponds to long in C, it is usually 32 bits.
• Double type:
-PHP's double type corresponds to C's double type.
-It can have a decimal point, exponent or both.
-There does not need to be any digits before or after the decimal point, both
are legal. EX: .345 345.
• String type:
-String literals are defined with either single (’) or double quotes (")
delimiters.
11. • In single quoted string literals, escape characters such as n, are not
recognized as anythingspecial and the values of embedded variables are
not substituted.
• In double-quoted string literals, escape sequences are recognized and
embedded variables arereplaced by their current values.
• Ex:
• Boolean type
Boolean values are either TRUE or FALSE, both of which are case
insensitive.
12. Output
• echo and print are both used to output data to the screen.
• echo has no return value and can take multiple parameters. print has return value
1 and cannot take multiple parmeters.
Ex:
13. Control Statements
• The control structures in PHP are very similar to C/C++/Java.
• The control statements include:
- if and if-else
-switch
- while and do-while
- for and foreach
14. If-elseif-else
• This statement executes different codes for more
than two conditions.
• EX: <?php
if ($x > $y)
{ echo "x is bigger than y"; }
elseif ($x == $y)
{ echo "x is equal to y"; }
else
{ echo "x is smaller than y"; }
?>
15. switch statement
• The switch statement is used to perform different action based on different
conditions.
• Example:
<?php
$x = 2;
switch ($x) {
case 1:printf("Choice is 1");
break;
case 2:printf("Choice is 2");
break;
case 3:printf("Choice is 3");
break;
default:printf("Choice other than 1, 2 and 3");
}
?>
16. While loop
• The while loop excute the
statement until the specified
condition is true.
• Ex:
<html>
<body>
<?php
$x = 1;
while($x <= 5) {
echo " $x ";
$x++;
}
?>
</body>
</html>
Do while loop
• It excutes the block of the code
and then check for the condition.
• Ex:
<html>
<body>
<?php
$x = 1;
do {
echo " $x ";
$x++;
} while ($x <= 5);
?>
</body>
</html>
17. For loop
• For loop is a control structure that repeats a block of code as long as a condition
is met. It's usually used to repeat a block of code a certain number of times.
• Example:
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
18. for-each
• The foreach loop works only on array and is used to loop through each
key/value pair in array.
• Example: