0% found this document useful (0 votes)
44 views22 pages

A Brief Introduction To PHP Language: Arjun Pratap Singh

This document provides an overview of PHP, a server-side scripting language commonly used for web development. It describes how PHP was created in 1995 and is used to create dynamic web pages. Key features outlined include being free, open-source, platform independent, and able to interface with databases like MySQL. The document also discusses PHP syntax, variables, operators, loops, functions, sessions, cookies, and how to connect to MySQL databases.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views22 pages

A Brief Introduction To PHP Language: Arjun Pratap Singh

This document provides an overview of PHP, a server-side scripting language commonly used for web development. It describes how PHP was created in 1995 and is used to create dynamic web pages. Key features outlined include being free, open-source, platform independent, and able to interface with databases like MySQL. The document also discusses PHP syntax, variables, operators, loops, functions, sessions, cookies, and how to connect to MySQL databases.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

A Brief Introduction to

PHP Language
by Arjun Pratap Singh
VII Sem. (I.T.)
What is PHP ?
 PHP: Hypertext Preprocessor.
 Server-side scripting languages for creating dynamic
Web pages.
 Originally created by Rasmus Lerdorf in 1995 known
as “personal home page”.
 Platform-independent.
 Also supports a large number of DB’s including
MySQL.
 PHP was written specifically for interacting with the
Web.
What is PHP ?
 PHP script file names usually ends with .php.
 PHP and MySQL are becoming increasingly popular in
recent years because it is free of charge. Other
software packages like Microsoft ASP.NET are
expensive for small and medium enterprises.
 PHP and MySQL are open-source; they can be
obtained free-of-charge and the source codes are
available for development.
– www.php.net
– www.mysql.com
Why PHP ?
 PHP runs on different platforms (Windows, Linux,
Unix, etc.).
 PHP is compatible with almost all servers used today
(Apache, IIS, etc.)
 Open source software (OSS).
 PHP is FREE to download and use.
 PHP is easy to learn and runs efficiently on the server
side.
How PHP is USED ?
Key Features of PHP
 High performance.
 Interface to many different database systems.
 Built-in libraries..
 Ease of learning and use.
 Object-oriented support.
 Portability.
 Open source.
 Free.
PHP v/s C++
 Similarities:
– Compiled Language
– Syntax nearly the same (For/While/If)
– Requires semicolons after each statement ;
– Assignment is right to left ($num = 56;)
– Object-Oriented (Class support, inheritance,
polymorphism)
– Functions!
– Types are nearly the same (boolean, integers,
strings, etc.)
PHP v/s C++
 Differences:
– Variables begin with $ sign ($name = “John Doe”;)
– No explicit declaration of variable types
– No Function Overloading
– “Hidden” functions-within-a-function
– Compiled/interpreted during every page load
– Documented!
– Echo for output
PHP - specifics
 Delimiters: <?php ?> or just <? ?>
 PHP parses code within delimiters
 Code outside delimiter sent to output, not parsed
 Block comments /* */
 Inline comments // #
A Simple PHP Example
<HTML>
<HEAD>
<TITLE>PHP Example</TITLE>
</HEAD>
<BODY>
<?
echo "<b>Welcome</b>"; // print the string here
?>
</BODY>
</HTML>
Variable in PHP
 variable is a symbol that can store different values at
different times.
 In PHP variable are loosely coupled data types so there is
no need to define data type before variable name in PHP
Variable Declaration
 Local variables =$num
 Global variables = Global $num
 Static variables = static $num
 Super Global Variables = $_VARNAME
 Dynamic Variables = $$myvar
PHP Operators
 Arithmetic Operator: ~,+,-,*,/,%
 Logical Operator: $$,||,!, and or xor
 Conditional Operator: <,>,<=,>=
 Unary Operator: ++,--
 Assignment Operators : =
 Equal Operators: ==
 Ternary Operator: expression?yes:no
LOOPS
In PHP, we have the following looping statements:
– while - loops through a block of code if and as long
as a specified condition is true
– do...while - loops through a block of code once, and
then repeats the loop as long as a special condition
is true
– for - loops through a block of code a specified
number of times
– foreach - loops through a block of code for each
element in an array
LOOPS
• while ( EXPR )
{ STATEMENTS; }
• do { STATEMENTS;
} while (EXPR);
• for ( INIT_EXPR; COND_EXPR; LOOP_EXPR )
{ STATEMENTS; }
• foreach (ARRAY as VARIABLE)
{ STATEMENTS; }
• break, continue
User-defined Functions
• Functions can be defined using the `function’ keyword.
<?php
function function_name($arg_1, $arg_2, ...)
{
   statements;
   return $some_value; // optional
}
?>
PHP SESSION
Session: session is a server variable which is used to keep
the user information on the server. It has its own life
cycle.
1.Initilize the session
2.Assign the value to the session
3.Use the session
4.Destroy the session
• Session_start(): is used to start the session.
• $_session[‘user’] :make a session variable.
• $_session[‘user’]=$varname;
• Unset($_session[‘user’]);
COOKIE
 What is cookie?
Cookie is a small text file which is used to
keep the user information in a client browser.
Cookie has its own life cycle similar to session.
1.Initialize the cookie
2.Assign the value to the cookie
3.Use the cookie
4.Destroy the cookie
COOKIE
• Setcookie() function is used to set a cookie.
syntax: setcookie(cookiename,value,expiretime)
• Retrieve a cookie value
the PHP $_COOKIE variable is used to retrieve a
cookie value.
syntax: $_COOKIE[‘COOKIE NAME’];
• Deleting cookie
Unset(); function is used to delete the cookie.
Mixing HTML and PHP Codes
• PHP codes can be easily inserted anywhere in an
HTML page.
• You can even mix the codes together, usually to avoid
writing too many “echo” statements with escape
characters.
<?
if ($i == 1) {
?>
<h2>The condition is true</h2>
<center><b>$i is 1</b></center>
<? HTML
PHP
} else {
?>
<h2>The condition is false</h2>
<center><b>$i is not 1</b></center>
<?
}
Technology Park, BDU
?>
CONNECTIVITY TO MYSQL
DATABASE
• Mysql_connect(“servername”,”serverusernam
e”,”password”);
• Mysql_select_db(db name);
• Sql=“select *from employee;”
• Mysql_query(sql );
• Mysql_affected_row();
• Mysql_num_row();
• Mysql_error();
Error Handling
 If there is error in the database connection, you can
terminate the current script by using the die function.
For example:
$db = mysql_connect("localhost", "root“, “”)
or die("Could not connect : " . mysql_error());
mysql_select_db("my_database")
or die("Could not select database");

$result = mysql_query($query)
or die("Query failed");
THANK YOU EVERYONE.

You might also like