You are on page 1of 8

Chap1: Introduction 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.

Web Development Environment: is environment that supports or helps PHP code to


execute on the local m/c in offline mode.
i) To develop the web application offline first we need to setup web development
environment in our local machine. i.e our computer system.
ii) On setting up web development environment, we convert our local machine to behave
as both client and server
iii) To set up web development environment we need to install one of following
packages-
iv) WAMP, LAMP, MAMP, XAMP
Where W=Windows , L=Linux, M-Mac OS, X=Any OS
Here we are going to use WAMP S/w package-
A= Apache Server
M=MySQL
P=PHP Engine
Explanation:
v) When server is installed on remote m/c then it is called Remote Host.
When server is installed on local m/c then it is called Local Host.
vi) Now local machine behaves like both server and client
vii) Adv : We donot need internet
viii) Once we complete web app then we can deploy website on server which is present on
remote m/c. In this way we can make website online.

1. Basic Syntax of PHP:

Syntax: <?php ?> “No space between ? and php

Note:

i) PHP script can be placed anywhere in doc


ii) Every stm in PHP is terminated by ;
iii) Default extension of PHP file is .php
iv) PHP file may contain php ,html and css code.

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

I: Must start with $ symbol

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 &nbsp.
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;

?>

Output: Parse Error

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.

The four types of PHP errors are:

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.

The most common causes of warning errors are:


 Calling on an external file that does not exist in the directory
 Wrong parameters in a function

For instance:
<?php

echo "Warning error"';

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 "Notice 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.

Parse Error (Syntax)


Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the
error and terminates the script.

Parse errors are caused by:


 Unclosed brackets or quotes
 Missing or extra semicolons or parentheses
 Misspellings

For example, the following script would stop execution and signal a parse error:

<?php

echo "Red";

echo "Blue";

echo "Green"
?>

It is unable to execute because of the missing semicolon in the third line.

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.

There are three (3) types of fatal errors:


1. Startup fatal error (when the system can’t run the code at installation)
2. Compile time fatal error (when a programmer tries to use nonexistent data)
3. Runtime fatal error (happens while the program is running, causing the code to stop working
completely)

For instance, the following script would result in a fatal error:

<?php

function sub()
{

$sub=6-1;

echo "The sub= ".$sub;

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.

You might also like