0% found this document useful (0 votes)
2 views7 pages

Lec11 PHP

PHP, originally known as Personal Home Page Tools, is a powerful server-side scripting language created in 1995 by Rasmus Lerdorf. It allows for the creation of dynamic web pages and database management, is open-source, and can be embedded within HTML. The document covers the history of PHP, basic coding rules, output instructions, and the use of constants and variables in PHP programming.

Uploaded by

mueed8bp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views7 pages

Lec11 PHP

PHP, originally known as Personal Home Page Tools, is a powerful server-side scripting language created in 1995 by Rasmus Lerdorf. It allows for the creation of dynamic web pages and database management, is open-source, and can be embedded within HTML. The document covers the history of PHP, basic coding rules, output instructions, and the use of constants and variables in PHP programming.

Uploaded by

mueed8bp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Introduction to PHP

1. PHP: Hypertext Preprocessor:


PHP stands for Hypertext Preprocessor. Originally it was called “Personal Home Page Tools”.
PHP started in 1995 when RasmusLerdorf who wrote a Perl/CGI script to know how many
visitors was reading his online résumé. This script was written to keep information about the
users and to display the count of visitors on the web. This initial success generated an interest
in web community. Lerdorf continued developing the language using C and giving away his
toolset, named Personal home page tools (PHP). In 1997 the PHP 2.0 was released, which
included a number of enhancements from programmers worldwide. This new version became
very popular so that by the release of PHP 3.0 in 1998, more than 50000 users used PHP for
enhancements in their websites.
In 2000, PHP4 was released. It added a number of enhancements including support for OOP,
better resource handling, support for sessions, data encryption etc. PHP5 added more by
providing vast support for OOP, exception handling, improved support for web services and
XML etc. PHP6 has been developed concurrently with PHP5.x.
PHP is a powerful server side scripting language. It is used to create dynamic web pages. It is
mainly used to manage database at server. It allows the web applications to communicate with
databases.
PHP is an open-source and platform independent language. Anyone may view, modify and
redistribute source code. It is an interpreted language; scripts are parsed at run-time rather
than compiled beforehand. It is compatible with many popular databases including Microsoft
SQL server, Oracle, SQL lite, and MySQL etc.
Embedding PHP with HTML:PHP can be embedded with HTML in three ways
 We can add blocks of PHP code in HTML
 We can use HTML instructions in PHP
 We can write standalone PHP script
Basic rules for writing PHP: following are some of the basic rules for writing PHP code
 Blocks of PHP code can be added in HTML code with opening and closing tags, as
follows:<?php and ?>. Similarly, we can use simply <? or ?> or <script
language=“PHP”>…… </script> to add PHP blocks in HTML
 PHP statements end with a semicolon
 Comments can be added as: // for one line comment and /* and */ for multiple lines
comment
Writing and executing PHP code: we follow the following steps to write and execute PHP code
 Using notepad or some advanced editor such as Dreamweaver write PHP code
 Save this code with .php extension
 Store all related web pages in on directory and copy this directory in WWW directory
which can be found at C:\wamp\www\
 Start WAMP server
 Select your web project (directory which contains web pages) in the list of your projects
on homepage of WAMP server
 Select the required web page to view its output
2. Output Instructions in PHP:
PHP provides several instructions to write output on the browser screen. Most commonly we
use echo to write output on the browser screen. The syntax of the echo is
echo (“Welcome to PHP”);
We can use also use echo command as echo “Welcome to PHP”;
print command can also be used to write out put on the browser. The syntax of writing print
command is print(“Welcome to PHP”); or print “Welcome to PHP”;
echo is marginally faster as compared to print as echo does not return any value. printf

function can also be used to output a formatted string.


First PHP program:in the following example we write our first PHP program. This program just
displays a welcome message on the browser
<html>
<head>
<title> First PHP program </title>
</head>
<body>
<?php
echo “Welcome to PHP”;
?>
</body>
</html>

Writing HTML in PHP: in the previous example, we have written PHP code in HTML. We can
also write HTML instructions in PHP. The echo statement outputs whatever it’s told to the
browser. It can output not only plain text but also HTML tags. We have to write HTML tags in
quotation marks. In PHP single and double quotation marks are used interchangeably but their
logical sequence is must to maintain. Following line shows how we can write HTML tags in PHP.
echo “<h1> Welcome to the PHP</h1>”;
In the following example, we have written HTML tags in PHP code.
<html>
<head>
<title>First PHP Program</title>
</head>
<body>
<?php
echo "<h1>Welcome to PHP</h1>";
echo "<h1 style='color:blue'>Welcome to PHP</h1>";
echo "<h1 style=\"color:red\">Welcome to PHP</h1>";
?>
</body>
</html>

In the above code we have displayed three headings on the browser screen. We have declared
HTML tags in PHP. The output of the program is shown in the following figure
3. Using constants and variables in PHP:
A constant is a placeholder for a value that you reference within your code that is formally
defined before using it. The name of a constant in PHP begins with a letter or an underscore.
Names of constants are case sensitive. Typically they are named using all capital letters. PHP
function define() is used to assign a value to a constant.In the following example we have
declared and used constant.
<html>
<head>
<title>Using Constants</title>
</head>
<body>
<?php
define("FAVGAME","Cricket");
echo "My favorite game is ";
echo FAVGAME;
?>
</body>
</html>

Variables in PHP:Variable is the name of a memory location which holds the data of a certain
type (data types). PHP supports all basic data types including integer, float, Boolean, string,
array, objet etc.
In PHP, variable names begin with $ sign. First character must be a letter or underscore while
remaining characters may be letters, numbers or underscores. Variable names are case
sensitive. We don’t need to declare or initialize variables. PHP is a loosely typed language it
means that data types does not require to be declare explicitly. The following example shows
the use of variables in PHP. In this example we have declared a variable and then, its value is
written on the browser window.
<html>
<head>
<title>Using Variables</title>
</head>
<body>
<?php
$favplayer = "Muhammad Yousuf";
echo "My favorite batsman is ";
echo $favplayer;
?>
</body>
</html>

As we discussed that PHP is a loosely typed language. The type of a variable is set to the type of
data assigned to it. However, PHP provides us functions which can be used to set the type of a
variable. Similarly, we can get the type of a variable using built-in PHP functions.
 The gettype() function returns the type of the provided variable
 The settype() function converts a variable to the type specified by type
The following example shows the use of gettypeanssettype functions
<html>
<head>
<title>Getting type</title>
</head>
<body>
<?php
$favnumber=10;
echogettype($favnumber)."<br>";
settype($favnumber,"string");
echogettype($favnumber);
?>
</body>
</html>

In the above code we have declared an integer variable and its type is displayed using gettype
function. Then we set the type of the variable to string and displayed it again. The output of the
above program is shown in the following figure
PHP provides us several functions which can be used to determine the type of a variable. These
functions are given below
is_array(): this function returns 1 (true) if the variable is of array type otherwise returns 0
is_bool(): this function returns 1 (true) if the variable is of Boolean type otherwise returns 0
is_float(): this function returns 1 (true) if the variable is of float type otherwise returns 0
is_integer():this function returns 1 (true) if the variable is of interger type otherwise returns 0
is_string(): this function returns 1 (true) if the variable is of string type otherwise returns 0
In the following example we have determined the variable type using these function and the
result is displayed on the screen
<html>
<head>
<title>type determination</title>
</head>
<body>
<?php
$favnumber=10;
$favplayer="Muhammad Yousuf";
echois_integer($favnumber)."<br>";
echois_string($favplayer)."<br>";
?>
</body>
</html>

The output is shown in the following figure

You might also like