PHP Notes 1 To 10
PHP Notes 1 To 10
Database access has been a prime focus of PHP development. It is integrated with
a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase,
Informix, and Microsoft SQL Server.
PHP supports a large number of major protocols such as POP3, IMAP, and LDAP.
PHP4 added support for Java and distributed object architectures (COM and
CORBA), making n-tier development a possibility for the first time.
PHP Syntax is C-Like or Perl-like or JavaScript-like. PHP file extensions can be
.php, .php3, .phtml etc.
PHP uses dynamic typing and hence PHP is far more forgiving. PHP is faster than
other scripting languages, for example, ASP and JSP.
PHP is free and an open source system. As on January 2019, PHP version is 7.3.1,
which is stable.
PHP scripts are either embedded in XHTML documents or are in files that are
referenced by XHTML documents.
All PHP code must be included inside one of the three special markup tags that
are recognised by the PHP Parser.
PHP can handle forms, i.e. gather data from files, save data to a file, through email
you can send data, return data to the user.
You add, delete, modify elements within your database through PHP.
Using PHP, you can restrict users to access some pages of your website.
Characteristics of PHP:
Five important characteristics that make PHP competent and powerful are:
Simplicity
Efficiency
Security
Flexibility
Familiarity
Web Server – PHP will work with virtually all Web Server software, including
Microsoft’s Internet Information Server (IIS) but then most often used is freely
available Apache Server.
Database – PHP will work with virtually all database software, including Oracle
and Sybase but most commonly used is freely available MySQL database.
Note: A PHP processor is now resident on most Web servers. WAMP, LAMP, XAMPP
are the currently used web server solution stacks.
Modes of Operation – PHP
PHP processor has 2 modes of operation – COPY Mode and INTERPRET Mode.
It takes a PHP document as input and produces XHTML document as output.
When PHP processor finds XHTML code in the input file, it simply copies it to the
output file.
When PHP processor encounters PHP script in the input file, it interprets it and
sends any output of the script to the output file.
In either case, it is evident that the output of PHP script is XHTML file or an
embedded client-side script.
Ex: Consider a PHP script is stored in a different file. It is brought to the XHTML
document with include construct, which takes the file name as it sparameter, as
shown below.
Include(“sample.php”);
The include construct causes the contents of the file sample.php to be copied into
the document where the call appears.
Here, the PHP interpreter changes from interpret to copy mode when an include
is encountered.
Hello, World!
If you examine the HTML output of the above example, you'll notice that the PHP
code is not present in the file sent from the server to your Web browser. All of the
PHP present in the Web page is processed and stripped from the page; the only
thing returned to the client from the Web server is pure HTML output.
Example 1: Example 2:
<!DOCTYPE html> <!DOCTYPE>
<html> <html>
<body> <body>
<?php <?php
echo "My first PHP script!"; echo "<h2>Hello by PHP</h2>";
?> ?>
</body> </body>
</html> </html>
PHP – General syntactic Characteristics
1. PHP syntax and semantics are closely related to syntax and semantics of
JavaScript, Perl.
2. PHP code is embedded in XHTML documents by enclosing it between
<?php and ?> tags.
3. PHP interpreter changes from interpret to copy mode when an include is
encountered.
Ex: Include(“sample.php”);
4. All variables in PHP begin with $ sign. Variable names are like in many common
programming languages; a letter or an underscore followed by any number of
letters, digits, underscores.
Ex: $sum, $str, $_name
5. PHP variable names are case sensitive.
<?php
$book = 67;
print("Variable book is $book);
print("Variable BooK is $BooK);
?>
11. Braces { } are used to form compound statements for control structures. Unless
used as body of a function, compound statements cannot be considered as blocks.
PHP –Primitives , Operations, Expressions
PHP has 4 scalar types namely Boolean, integer, double and string;
has 2 compound types namely array and object ; 2 special types namely resource
and NULL.
PHP Variables:
Double Type:
Doubles in PHP corresponds to the double type of C language.
Doubles include a decimal point, exponent, or both.
Ex: $many = 2.2800;
$many_2 = .2115;
Boolean Type:
The only two possible values for Boolean type are TRUE or FALSE, which are
case insensitive.
if (TRUE)
print("This will always print");
else
print("This will never print");
Expressions of other types can be used in Boolean context. If we use non-
Boolean expression in Boolean context, we should its interpretation.
Here are the rules for determine the "truth" of any value not already of the
Boolean type:
a) If the value is a number: it is FALSE if exactly equal to zero and TRUE
otherwise.
b) If the value is a string: it is FALSE if the string is empty (has zero
characters) or is the string "0", and is TRUE otherwise.
c) Values of type NULL are always FALSE.
d) If the value is an array, it is FALSE if it contains no other values, and it is
TRUE otherwise. For an object, containing a value means having a member
variable that has been assigned a value.
e) Valid resources are TRUE (although some functions that return resources
when they are successful will return FALSE when unsuccessful).
f) Don't use double as Booleans.
Each of the following variables has the truth value embedded in its name when
it is used in a Boolean context.
g) $false_str = "";
String Type:
Example:
<?php
$sum = 2003;
$literally = 'My Value of sum is:$sum';
print($literally);
$literally = "My Value of sum is:$sum";
print($literally);
?>
This will produce following result −
There are no artificial limits on string length – can have any length within the
bounds of available memory.
Strings that are delimited by double quotes (as in "this") are preprocessed in the
following two ways by PHP:
a) Certain character sequences beginning with backslash (\) are replaced with
special characters
b) Variable names (starting with $) are replaced with string representations of their
values.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators:
PHP has the usual collection of arithmetic operators, like C- language, with the
usual meanings.
echo(floor(-5.9)); -6
echo(floor(0.60)); 0
echo(floor(5.1)); 5
echo(floor(-5.1)); -6
echo(ceil(-5.9)); -5
echo(ceil(0.60)); 1
echo(ceil(5.1)); 6
echo(ceil(-5.1)); -5
echo(round(0.50)); 1
echo(round(0.49)); 0
echo(round(-4.40)); -4
echo(round(-4.60)); -5
srand(10); 10
echo(abs(-3.6)); 3.6
echo(max(22,14,68,18,15); 68
echo(max(array(44,16,81,12))); 81
echo(min(22,14,68,18,15)); 14
echo(min(array(44,16,81,12))); 12
String Operations:
The only string operator is the concatenation operator specified with a period (.)
Strings variables can be treated like arrays for access to individual cjharacters.
The position of a character in s string, relative to zero, can be specified in braces
immediately after the variable’s name.
Ex: “book”, $str{3} is “k”
PHP includes many functions that operate on strings. Some of the most commonly
used of these are as below.
Function Parameter Type Return Value
strlen() A string Number of characters in the string. Length of the
string.
strcmp() Two strings Compares two strings (case-sensitive). Zero if
strings are identical, negative number if the first
string belongs before second string (in ASCII
sequence), positive number if the second string
belongs before the first string.
strpos() Two strings Returns the position of the first occurrence of a
string inside another string (case-sensitive)
substr() A string and an The substring of the string parameter, starting
integer from the position indicated by the second
parameter.
chop() A string, Removes whitespace characters from the right
Characterlist end of a string. Second parameter specifies the
characters to be removed.
trim() A string, Removes whitespace characters from both sides
Characterlist of a string. Second parameter specifies the
characters to be removed.
ltrim() A string, Removes whitespace characters from the left
Characterlist side of a string. Second parameter specifies the
characters to be removed.
strtolower() A string Converts a string to lowercase letters
echo strlen("Hello"); 5