Professional Documents
Culture Documents
Introduction to PHP:
HP is a server scripting language, and a powerful tool for making dynamic
and interactive Web pages.
At its very simplest, a PHP document will output only HTML. To prove
this, you can take any normal HTML document, such as an index.html file,
and save it as index.php; it will display identically to the original.
<?php
echo "Hello world";
?>
Structure of PHP
A PHP script can be placed anywhere in the document.
<?php
// PHP code goes here
?>
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
operator in php:
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
Arithmetic Operators
There are following arithmetic operators supported by PHP language −
Assume variable A holds 10 and variable B holds 20 then −
Comparison Operators
There are following comparison operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then −
== Checks if the value of two operands are equal or not, if yes then (A == B) is not
condition becomes true. true.
!= Checks if the value of two operands are equal or not, if values are not (A != B) is
equal then condition becomes true. true.
> Checks if the value of left operand is greater than the value of right (A > B) is not
operand, if yes then condition becomes true. true.
< Checks if the value of left operand is less than the value of right (A < B) is true.
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value (A >= B) is not
of right operand, if yes then condition becomes true. true.
<= Checks if the value of left operand is less than or equal to the value of (A <= B) is
right operand, if yes then condition becomes true. true.
Logical Operators
There are following logical operators supported by PHP language
and Called Logical AND operator. If both the operands are true then (A and B) is
condition becomes true. true.
or Called Logical OR Operator. If any of the two operands are non zero (A or B) is
then condition becomes true. true.
&& Called Logical AND operator. If both the operands are non zero then (A && B) is
condition becomes true. true.
|| Called Logical OR Operator. If any of the two operands are non zero (A || B) is
then condition becomes true. true.
! Called Logical NOT Operator. Use to reverses the logical state of its !(A && B) is
operand. If a condition is true then Logical NOT operator will make false.
false.
Assignment Operators
There are following assignment operators supported by PHP language −
= Simple assignment operator, Assigns values from right side C = A + B will assign
operands to left side operand value of A + B into C
Conditional Operator
There is one more operator called conditional operator. This first evaluates an
expression for a true or false value and then execute one of the two given statements
depending upon the result of the evaluation. The conditional operator has this syntax
Operators Categories
All the operators we have discussed above can be categorised into following
categories −
• Unary prefix operators, which precede a single operand.
• Binary operators, which take two operands and perform a variety of arithmetic
and logical operations.
• The conditional operator (a ternary operator), which takes three operands and
evaluates either the second or third expression, depending on the evaluation
of the first expression.
• Assignment operators, which assign a value to a variable.
The echo statement can be used with or without parentheses: echo or echo().
Display Text
The following example shows how to output text with the echo command
(notice that the text can contain HTML markup):
Example
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Display Text
The following example shows how to output text with the print command
(notice that the text can contain HTML markup):
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP functions: A function is a piece of code which takes one more input in the form
of parameter and does some processing and returns a value.
There are two parts which should be clear to you −
<head>
<title>Writing PHP Function</title>
</head>
<body>
<?php
/* Defining a PHP Function */
function writeMessage() {
echo "You are really a nice person, Have a nice
time!";
}
</body>
</html>
<head>
<title>Writing PHP Function with Parameters</title>
</head>
<body>
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
</body>
</html>
<head>
<title>Writing PHP Function which returns value</title>
</head>
<body>
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, 20);
</body>
</html>
<html>
<head>
<title>Dynamic Function Calls</title>
</head>
<body>
<?php
function sayHello() {
echo "Hello<br />";
}
$function_holder = "sayHello";
$function_holder();
?>
</body>
</html>
• local
• global
• static
Example
Variable with global scope:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
Example
Variable with local scope:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
To do this, use the static keyword when you first declare the variable:
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
TRUE or FALSE?
A basic Boolean value can be either TRUE or FALSE. For example, the
expression “20 > 9” (20 is greater than 9) is TRUE, and the expression “5
== 6” (5 is equal to 6) is FALSE
Flow-Control Statements
if
The if statement checks the truthfulness of an expression and, if the
expression is true, evaluates a statement. An if statement looks like:
if (expression)
statement
To specify an alternative statement to execute when the expression is false,
use the else keyword:
if (expression)
statement
else
statement
For example:
if ($user_validated)
echo "Welcome!";
else
Conditional Operator
There is one more operator called conditional operator. This first evaluates an
expression for a true or false value and then execute one of the two given statements
depending upon the result of the evaluation. The conditional operator has this syntax
−
<head>
<title>Arithmetical Operators</title>
</head>
<body>
<?php
$a = 10;
$b = 20;
</body>
</html>
Break
You have already seen the break statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
— the include() statement will only generate a PHP warning but allow
script execution to continue if the file to be included can't be found, whereas
---the require() statement will generate a fatal error and stops the script
execution.
Syntax
include 'filename';
or
require 'filename';
EXAMPLE:
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>