Hehehehheheh: Finally, PHP Is Easy. If You Know C or Perl, Learning PHP Is A Cinch. The Language Is A
Hehehehheheh: Finally, PHP Is Easy. If You Know C or Perl, Learning PHP Is A Cinch. The Language Is A
PHP is a server-side scripting language designed specifically for the Web. Within an HTML
page, we can embed PHP code that will be executed each time the page is visited.HTML
generates the web page with the static text and images. However the need evolved for dynamic
web based application, mostly involving database usage. These dynamic usages are facilated by
PHP. Other tasks that PHP is especially good at are database access, disk access, networking and
text manipulation. PHP is an excellent alternative to such similar programming solutions as
Microsoft's proprietary scripting engine ASP and Allaire's rather expensive ColdFusion. As
mentioned before, PHP is a cross-platform language.
Finally, PHP is easy. If you know C or Perl, learning PHP is a cinch. The language is a
mix between the two, taking the best features from both. Plus PHP adds features to solve
common problems that programmers often encounter when programming for the Web.
CONTENTS
ACKNOWLEDGEMENT.5
INTRODUCTION.6
1. PHP Syntax.7
1.1 Basic PHP Syntax.7
2. Comments in PHP....8
3. PHP variable..9
3.1 Variables In PHP...9
3.2 PHP is a loosely typed language...9
3.3 Naming rules for variable.9
4. PHP string ....10
4.1 String variable in PHP...10
4.2 The concatenation operatar...10
4.3 The strlen() function..11
4.4 the strpos() function11
5. PHP echo ...12
6. Conditional statements..12
6.1 Numeric arrays13
6.2 Associative arrays13
6.3 Multidimensionol arrays.....14
7. Arrays in PHP.14
7.1 The if statement..14
7.2 The ifelse statement....15
7.3 The ifelse if.else statement.16
7.4 Switch statement .16
Acknowledgement
I express my sincere thanks to my guide Mr. Manish Shandilya (Web page developer in PHP,
Bikaner), for guiding me right from the inception till the successful completion. I sincerely
acknowledge him for extending his valuable guidance, support for literature, critical reviews on
PHP (PHP: Hypertext Preprocessor) and the report and above all the moral support he had
provided me with all stages of this topic. Thanks also too many people who provided detailed
reviews on the topic PHP (PHP: Hypertext Preprocessor) and thanks also to the people who are
responsible for the publication of the report.
Finally, with all this assistance, little remains for which I can take full credit.
ROHIT DUGGAL
Computer Science & Engg.
VII semester, CET
INTRODUCTION
1. PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the browser.
<?php
?>
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser:
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to
distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. In the example above we
have used the echo statement to output the text "Hello World".
Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be
executed.
2. Comments in PHP
In PHP, there are two types of comments used here. These types are as follows:
Single line comment :
In this we use use // to make a single-line comment
<html>
<body>
<?php
//This is a single line comment
?>
</body>
</html>
<html>
<body>
<?php
/*
This is a multiline
Comment block
*/
?>
</body>
</html>
3. PHP Variables
A variable is used to store information. A variable is a means of storing a value, such as text
string "Hello World!" or the integer value 4. A variable can then be reused throughout your code,
instead of having to type out the actual value over and over again. Also, variable names are casesensitive, so use the exact same capitalization when using a variable. The variables $a_number and
$A_number are different variables in PHP's eyes.
3.1 Variables in PHP
All variables in PHP start with a $ sign symbol. The correct way of declaring a variable in
PHP:
$var_name = value;
New PHP programmers often forget the $ sign at the beginning of the variable. In that case
it will not work. Let's try creating a variable containing a string, and a variable containing a
number:
<?php
$txt="Hello World!";
$x=16;
?>
4. PHP String
A string variable is used to store and manipulate text.
4.1 String Variables in PHP
String variables are used for values that contain characters. In this chapter we are going to look at
the most common functions and operators used to manipulate strings in PHP. After we create a
string we can manipulate it. A string can be used directly in a function or it can be stored in a
variable. Below, the PHP script assigns the text "Hello World" to a string variable called $txt:
<?php
$txt="Hello World";
echo $txt;
?>
The output of the code above will be:
Hello World
If we look at the code above you see that we used the concatenation operator two times. This is
because we had to insert a third string (a space character), to separate the two strings.
12
The length of a string is often used in loops or other functions, when it is important to know
when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the
string).
The position of the string "world" in our string is position 6. The reason that it is 6 (and not 7), is
that the first position in the string is 0, and not 1.
5. PHP- Echo
The PHP command echo is a means of outputting text to the web browser. Throughout your
PHP career you will be using the echo command more than any other.
To output a string, like we have done in previous lessons, use PHP echo. You can place either
a string variable or you can use quotes, like we do below, to create a string that the echo function
will output.
<?php
$txt="Hello World!";
echo $txt; echo <h5>Using ECHO</h5>;
?>
The output for above program is:
Hello World!
Using ECHO
In the above example we output "Hello World!" without a hitch. The text we are outputting is
being sent to the user in the form of a web page, so it is important that we use proper HTML
syntax!
In our second echo statement we use echo to write a valid Header 5 HTML statement. To do
this we simply put the <h5> at the beginning of the string and closed it at the end of the string.
Just because you're using PHP to make web pages does not mean you can forget about HTML
syntax!
6. Arrays in PHP
An array stores multiple values in one single variable. The problem is, a variable will hold only
one value. An array is a special variable, which can store multiple values in one single variable.
In PHP, there are three kind of arrays:
In the above example we made use of the index / value structure of an array. The index was the
numbers we specified in the array and the values were the names of the cars. Each index of an
array represents a value that we can manipulate and reference. The general form for setting the
index of an array equal to a value is:
$array[index] = value;
7. Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
if statement - use this statement to execute some code only if a specified condition is true
if...else statement - use this statement to execute some code if a condition is true and
another code if the condition is false
if...else if....else statement - use this statement to select one of several blocks of code to
be executed
switch statement - use this statement to select one of many blocks of code to be executed
Now, let us discuss these one after other:
7.1 The if Statement
The if statement is used to execute some code only if a specified condition is true. Syntax for if
statement is given below.
Syntax
if (condition) code to be executed if condition is true;
EXAMPLE
The following example will output "Have a nice weekend!" if the current day is Friday:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The following example will output "Have a nice weekend!" if the current day is Friday,
otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
Example
The following example will output "Have a nice weekend!" if the current day is Friday, and
"Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
This is how it works: First we have a single expression n (most often a variable), that is
evaluated once. The value of the expression is then compared with the values for each case in the
structure. If there is a match, the block of code associated with that case is executed. Use break
to prevent the code from running into the next case automatically. The default statement is used
if no match is found.
Example
<html>
<body>
<?php
switch ($x)
{
case 1:
8. PHP Functions
In PHP, there are more than 700 built-in functions. To keep the browser from executing a script
when the page loads, you can put your script into a function. A function will be executed by a
call to the function. You may call a function from anywhere within a page.
8.1 Create a PHP Function
A function will be executed by a call to the function.
Syntax
function functionName()
{
code to be executed;
}
Give the function a name that reflects what the function does
Example
<html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
</body>
</html>
Output:
My name is Kai Jim Refsnes
number)
1 + 16 = 17
When the user clicks the "Submit" button, the URL sent to the server could look something like
this:
The "welcome.php" file can now use the $_GET function to collect form data (the names of the
form fields will automatically be the keys in the $_GET array):
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
When the user clicks the "Submit" button, the URL will look like this:
http://www.w3schools.com/welcome.php
The "welcome.php" file can now use the $_POST function to collect form data (the names of the
form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
Because the variables are not displayed in the URL, it is not possible to bookmark the page.
But it is possible in GET function, as the variables are appeared in URL.
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
Output:
The
The
The
The
The
number
number
number
number
number
is
is
is
is
is
1
2
3
4
5
{
code to be executed;
}
while (condition);
EXAMPLE
The example below defines a loop that starts with i=1. It will then increment i with 1, and write
some output. Then the condition is checked, and the loop will continue to run as long as i is less
than, or equal to 5:
<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>
Output:
The
The
The
The
The
number
number
number
number
number
is
is
is
is
is
2
3
4
5
6
Parameters:
init: Mostly used to set a counter (but can be any code to be executed once at the
beginning of the loop)
Condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues.
If it evaluates to FALSE, the loop ends.
increment: Mostly used to increment a counter (but can be any code to be executed at the
end of the loop)
The common tasks that are covered by a for loop are:
1. Set a counter variable to some initial value.
2. Check to see if the conditional statement is true.
3. Execute the code within the loop.