Chap1 Intro To PHP
Chap1 Intro To PHP
1. History of PHP
2. Features of PHP
3. Client Server Architecture.
4. Web development
5. Installation
6. Basic Syntax
7. Error_reporting(0)
History of PHP:
To create real time dynamic web sites in the year 1994 Rasmus Lerdorf developed PHP at Zend
Technologies.
i) Initially he wrote PHP to maintain his personal home page. i.e blogs.
ii) Later extended it to communicate with databases, file systems and work with web
forms. This implementation as PHP/FI (Personal Home Page/ Forms Interpreter).
iii) Later it was changed to Hypertext preprocessor.
iv) PHP code is executed by PHP engine available at server m/c Hence server-side
scripting
v) Stable and latest Release: PHP 7.1.6 (2017)
Features of PHP:
1. It is server-side scripting lang.i.e. it is executed on server side.
2. Simple and ease to use : Easy to learn and popular for its simplicity
3. Open source and free: Code is available on website where any one do changes in it.
4. It is loosely typed language -No var declaration is required.
5. It is case sensitive same like c : All keywords are case sensitive and must be written in
small letters and var are case sensitive i.e AGE and age are different.
6. Runs on various platforms i.e. os
7. Supports wide range of dbs like mysql, oracle,Sybase etc.
8. Error reporting and exceptions: PHP5 supports exception handling which is used to throw
error and which can be caught any time.
Client Server Architecture:
1. Here client and server is connected by internet and www where internet is H/W and
WWW is s/w
2. What is client ? Any pc, laptop or mobile which is connected to internet and which is
responsible or able to generate the request is called client. Client means local machine.
3. What is server ? Server is s/w or h/w device that accepts and responds to requests made
over internet
4. How we send request through client ? Within each client we have browsers like chrome,
mozilla is installed. Through these browsers we sent request to server via internet
5. Servers locate or finds the page and send it as response to client, that page will be
displayed or rendered on browsers at client side.
Note:
2. PHP Comments:
i) A comment in PHP code is never executed, its sole purpose is to understand the code.
There are various ways of commenting statements in PHP code:
ii) There are different ways of commenting
a) Single Line Comment : // or #
b) Multiline Comment : /* …*/
3. PHP Variables:
i) Defn, purpose
ii) Syntax: $varnm=val;
Rules
II: Must start with letter or underscore and cant start with number
III. Varnms are case sensitive
1. Echo construct:
i) It is used to display output on web browser
ii) It is used display string values of variable.
iii) Echo can be used with or without parenthesis
iv) It is language construct and not function so it cannot be used in expression.
v) Syntax:
a) echo “Msg1”,”Msg2”….”Msgn”;
b) echo (“Msg”);
Ex:
a) echo “Hi”,”Bye”,”How”,1; // O/p as it is
b) Echo(“Hi”, ” Bye”, How”); //Error
Note:
i) It is not recommended to use echo without brackets as () version disp only single
msg at a time.
ii) When u use single quote variable names are displayed as it is.
iii) To add space in PHP code use  .
iv) To add line breakup use <br>. We can insert HTML tags in between “ “ as part of
PHP string to generate desired HTML structure.
2. Print Statement:
i) Print is also a statement i.e used to display the output. it can be used with
parentheses print( ) or without parentheses print.
ii) using print can doesn't pass multiple argument
iii) print always return 1
iv) it is slower than echo
EXAMPLE:
<?php
$nm=”John”;
$ret=print $nm;
echo $ret;
?>
Output: John
<?php
$nm=”John”;
$ret=echo $nm;
echo $ret;
?>
Error in PHP
A PHP Error occurs when something is wrong in the PHP code. The error can be as simple as a
missing semicolon, or as complex as calling an incorrect variable.
To efficiently resolve a PHP issue in a script, you must understand what kind of problem is
occurring.
1. Warning Error
2. Notice Error
3. Parse Error
4. Fatal Error
Warning Error
A warning error in PHP does not stop the script from running. It only warns you that there is a
problem, one that is likely to cause bigger issues in the future.
For instance:
<?php
include ("external_file.php");
?>
As there is no “external_file”, the output displays a message, notifying it failed to include it.
Still, it doesn’t stop executing the script.
Notice Error
Notice errors are minor errors. They are similar to warning errors, as they also don’t stop code
execution. Often, the system is uncertain whether it’s an actual error or regular code. Notice
errors usually occur if the script needs access to an undefined variable.
Example:
<?php
$a="Defined error";
echo $b;
?>
In the script above, we defined a variable ($a), but called on an undefined variable ($b). PHP
executes the script but with a notice error message telling you the variable is not defined.
For example, the following script would stop execution and signal a parse error:
<?php
echo "Red";
echo "Blue";
echo "Green"
?>
Fatal Error
Fatal errors are ones that crash your program and are classified as critical errors. An undefined
function or class in the script is the main reason for this type of error.
<?php
function sub()
{
$sub=6-1;
div();
?>
The output tells you why it is unable to compile, as in the image below:
Conclusion
Distinguishing between the four types of PHP errors can help you quickly identify and solve
problems in your script. Make sure to pay attention to ouput messages, as they often report on
additional issues or warnings.
Error_reporting(0)- By passing zero (0) in error_reporting function, you can remove all errors,
warnings, notices, and parse messages. It would be much better to turn off report messages
in .htaccess or in ini file instead of having this code in each or every PHP file.
error_reporting(0);
PHP allows developers to use undeclared variables. But these undeclared variables can cause
problems for the application when used in conditions and loop.
Sometimes, this could happen that the variable which is declared and being used in conditions or
loops have different spellings. So, to display the undeclared variable in the web application, pass
the E_NOTICE in the error_reporting function.