Php
Php
History
PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995.
PHP 7.4.0 is the latest version of PHP, which was released on 28 November.
PHP Features
PHP is very popular language because of its simplicity and open source. There
are some important features of PHP given below:
Performance:
PHP script is executed much faster than those scripts which are written in other
languages such as JSP and ASP. PHP uses its own memory, so the server
workload and loading time is automatically reduced, which results in faster
processing speed and better performance.
Open Source:
PHP source code and software are freely available on the web. You can develop
all the versions of PHP according to your requirement without paying any cost.
All its components are free to download and use.
PHP code can be easily embedded within HTML tags and script.
Platform Independent:
PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP
application developed in one OS can be easily executed in other OS also.
Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
Error Reporting -
PHP allows us to use a variable without declaring its datatype. It will be taken
automatically at the time of execution based on the type of data it contains on its
value.
PHP is compatible with almost all local servers used today like Apache, Netscape,
Microsoft IIS, etc.
Security:
Control:
Different programming languages require long script or code, whereas PHP can
do the same work in a few lines of code. It has maximum control over the
websites like you can make changes easily whenever you want.
Web Development
PHP is widely used in web development nowadays. PHP can develop dynamic
websites easily. But you must have the basic the knowledge of following
technologies for web development as well.
o HTML
o CSS
o JavaScript
o Ajax
o XML and JSON
o jQuery
Prerequisite
Before learning PHP, you must have the basic knowledge of HTML,
CSS, and JavaScript. So, learn these technologies for better implementation of
PHP.
CSS - CSS helps to make the webpage content more effective and attractive.
Audience
Our PHP tutorial is designed to help beginners and professionals. This PHP
tutorial will help those who are unaware about the concepts of PHP but have
basic knowledge of computer programming.
Problem
We assure you that you will not find any problem in this PHP tutorial. But if there
is any mistake or error, please post the error in the contact form.
Install PHP
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP)
software stack. It is available for all operating systems. There are many AMP
options available in the market that are given below:
AMP For
WAMP Windows
LAMP Linux
MAMP Mac
SAMP Solaris
FAMP FreeBSD
XAMPP (Cross, Apache, MySQL, PHP, Perl)
for Cross Platform: It includes some
other components too such as
FileZilla, OpenSSL, Webalizer,
Mercury Mail, etc.
Installation of XAMPP
To do so, create a file and write HTML tags + PHP code and save this file
with .php extension.
Note: PHP statements ends with semicolon (;).
All PHP code goes between the php tag. It starts with <?php and ends with ?>.
The syntax of PHP tag is given below:
1. <?php
2. //your code here
3. ?>
Let's see a simple PHP example where we are writing some text using PHP echo
command.
File: first.php
1. <!DOCTYPE>
2. <html>
3. <body>
4. <?php
5. echo "<h2>Hello First PHP</h2>";
6. ?>
7. </body>
8. </html>
Output:
As I'm using window, and my XAMPP server is installed in D drive. So, the path
for the htdocs directory will be "D:\xampp\htdocs".
PHP program runs on a web browser such as - Chrome, Internet Explorer,
Firefox, etc. Below some steps are given to run the PHP programs.
1. <?php
2. echo "Hello World!";
3. ?>
Step 2: Save the file with hello.php name in the htdocs folder, which resides
inside the xampp folder.
Note: PHP program must be saved in the htdocs folder, which resides inside
the xampp folder, where you installed the XAMPP. Otherwise it will generate
an error - Object not found.
Step 3: Run the XAMPP server and start the Apache and MySQL.
Step 5: The output for the above hello.php program will be shown as the
screenshot below:
Most of the time, PHP programs run as a web server module. However, PHP can
also be run on CLI (Command Line Interface).
Language Work Return Function Info Example
Construct any
value
echo display the Nothing Not We can pass multiple strings separated by a comma (,) in echo. <?php
output echo "Hello by PHP echo";
?>
Echo is faster than the print statement.
print display the 1 Not print can be used with or without parentheses. <?php
output. print "Hello by PHP print ";
print ("Hello by PHP print()");
Using print, we cannot pass multiple arguments.
?>
$ Declare variable Nothing Not do not need to declare the data types of the variables. <?php
$str="hello string";
$x=200;
Contain A-z, 0-9, _
$y=44.6;
echo "string is: $str <br/>";
Start with A-z, _ echo "integer is: $x <br/>";
echo "float is: $y <br/>";
No space ?>
Case-sensitive
Local declared within a A variable declaration outside the function with the same name is <?php
variable function completely different function local_var()
{
$num = 45;
variables cannot be accessed outside the function
echo "Local variables value
is: ". $num;
local variable has higher priority than the global variable }
local_var();
?>
Global declared outside These variables can be accessed anywhere in the program. <?php
variable the function
use the GLOBAL keyword before the variable. $name = "Sanaya
Sharma"; //Global Variable
Or $GLOBALS
function global_var()
global $name;
echo "Variable
inside the function: ". $name;
echo "</br>";
global_var();
echo "Variable
outside the function: ". $name;
?>
S Static <?php
c variable
o
function static_var()
p
e
o {
f
v static $num3 = 3;
a //static variable
ri
a
b $num4 =
l 6; //Non-static variable
e
//increment in
non-static variable
$num3++;
//increment in
static variable
$num4++;
//first
function call
static_var();
//second function
call
static_var();
?>
$$ <?php
$x = "abc";
$$x = 200;
echo $x."<br/>";
echo $$x."<br/>";
echo $abc;
?>
Definitions
Php Php is a server-side scripting language, which is used to design the dynamic web 1
applications with mysql database.
Hypertext Text displayed on a computer display or other electronic devices with 1
references (hyperlinks) to other text that the reader can immediately access.
Preprocessor A program that processes its input data to produce output that is used as input 1
in another program
Object-oriented Enables a system to be modelled as a set of objects which can be controlled 1
and manipulated in a modular manner.
Interpreted A language in which the implementations execute instructions directly without 1
earlier compiling a program into machine language.
Server-side Server-side scripting is a technique used in web development which involves 1
employing scripts on a web server which produces a response customized for
each user's (client's) request to the website.
Open source Freely available 1
Redistributed
Modified
Rasmus Lerdorf PHP was conceived 1