Lec11 PHP
Lec11 PHP
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>