Lecture 3 PHP
Lecture 3 PHP
</body>
</html>
Variables in PHP
All variables in PHP start with a $ sign symbol.
$var_name = value;
<?php
$txt="Hello World!";
$x=16;
?>
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data
type the variable is.
PHP automatically converts the variable to the correct data type,
depending on its value.
In a strongly typed programming language, you have to declare (define)
the type and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.
Naming Rules for Variables
A variable name must start with a letter or an
underscore "_"
A variable name can only contain alpha-
numeric characters and underscores (a-z, A-Z,
0-9, and _ )
A variable name should not contain spaces. If
a variable name is more than one word, it
should be separated with an underscore
($my_string), or with capitalization
($myString)
String Variables in PHP
String variables are used for values that
contains 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.
<?php
$txt="Hello World";
echo $txt;
?>
The Concatenation Operator
The concatenation operator (.) is used to
put two string values together.
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
The strlen() function
<?php
echo strlen("Hello world!");
?>
The strpos() function is used to search for
character within a string.
If a match is found, this function will return
the position of the first match. If no match is
found, it will return FALSE.
<?php
echo strpos("Hello world!","world");
?>
PHP Operators
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>
The if...else Statement
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
The if...elseif....else Statement
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
The PHP Switch Statement
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from
both label1 and label2;
}
<html>
<body>
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
The while Loop
while (condition)
{
code to be executed;
}
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
The do...while Statement
do
{
code to be executed;
}
while (condition);
<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>
The for Loop
for (init; condition; increment)
{
code to be executed;
}
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>
The foreach loop is used to loop through arrays.
foreach ($array as $value)
{
code to be executed;
}
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>
PHP Arrays
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric
index
Associative array - An array where each ID
key is associated with a value
Multidimensional array - An array
containing one or more arrays
Numeric Arrays
$cars=array("Saab","Volvo","BMW","Toyota");
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish
cars.";
?>
Associative Arrays
each ID key is associated with a value.
$ages = array("Peter"=>32, "Quagmire"=>30,
"Joe"=>34);
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
fclose($file);
?>
The fwrite() writes to an open file.
The function will stop at the end of the file or
when it reaches the specified length, whichever
comes first.
This function returns the number of bytes
written, or FALSE on failure.
fwrite(file,string,length)
The fputs() writes to an open file.
fputs(file,string,length)
Php file creation
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or exit("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);
?>
Read the file
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
Append data in php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open
file");
$stringData = "New Stuff 1\n"; fwrite($fh,
$stringData);
$stringData = "New Stuff 2\n"; fwrite($fh,
$stringData);
fclose($fh);