Professional Documents
Culture Documents
Basics of PHP - 1
Basics of PHP - 1
3. PHP basic syntax
IMPORTANT: PHP code is case-sensitive (like JavaScript) but HTML is not case-sensitive.
3.1 How to write PHP in HTML
Begin with <?php , and end with ?>.
<?php
........PHP code...........
?>
3.2 Where to insert PHP script in HTML?
You can insert PHP script basically anywhere in HTML page. There is no limitation of the
location.
<HTML>
<HEAD>
</HEAD>
<BODY>
</BODY>
</HTML>
Sometimes it is needed
to place PHP code
before <HTML> tag.
IMPORTANT: The file must be saved with the extension of .php, not .html
statement;
Variable declaration
There is no need to declare variables in PHP. You can just start using variables anywhere in
the code.
IMPORTANT: Variable name in PHP must begin with $.
Example) $age = 25;
Simple branch
if (condition) {
...
}
Same as JavaScript
ADMTC-UCSC-University of Colombo
Basics of PHP - 1
Multiple branch
if (condition1) {
...
} elseif (condition2) {
...
} else {
...
}
Same as JavaScript
Same as JavaScript
Function
function (variable_1, variable_2, , variable_n) {
...
}
Same as JavaScript
Comments
// one-line comment
/*
Multi-line comment
*/
Operators
$a = 5 + 2;
$a = 5 - 2;
$a = 5 * 2;
$a = 5 / 2;
$a = "Hello, " . "PHP";
// Add
// Subtract
// Multiply
// Divide
// String concatenation
$a is a variable
ADMTC-UCSC-University of Colombo
Basics of PHP - 1
(Example 2)
<HTML><BODY>
<?php
variable
$age = 35;
echo "<H1>I am $age years old</H1>n";
?>
</BODY></HTML>
date() function
Get current date and time as a string according to the specified format.
$now = date("d/m/Y h:m:s");
echo "<P>Current date and time is $now</P>n";
5. Arrays
Array is a special variable that can store multiple values. Array is very convenient in many
ways, and you must know how to use arrays when you access to database.
ADMTC-UCSC-University of Colombo
Basics of PHP - 1
foreach loop
Special version of for loop that can be used with arrays. You dont have to specify index in
the loop.
$a["name"] = "UCSC";
$a["age"] = 41;
$a["office"] = "ADMTC";
foreach ($a as $key => $value) {
echo "$key: $value<BR> ";
}
ADMTC-UCSC-University of Colombo
Basics of PHP - 1
http://server_name/page_name.php?variable_name=value
You can use this parameter in PHP code to change the behavior.
setoption.html (Normal HTML page to send parameter to PHP page)
<HTML>
<BODY>
<p><a href="getoption.php?option=1">Click here for option 1</a>
<p><a href="getoption.php?option=2">Click here for option 2</a>
</BODY>
Add name of the variable and its value to URL
</HTML>
getoption.php (PHP page to receive the option from above page)
<HTML>
Name of the variable
<BODY>
<p>You click the option <?php echo $_GET["option"]; ?></p>
</BODY>
</HTML>
If you want to use many parameters, you can separate each parameter by & character as
follows.
http://server_name/page_name.php?
variable_name=value&variable_name=value
6.3 Getting the response from user using URL parameters
JavaPHP.php
<HTML>
<BODY>
<P>Enter the year you born</P>
<P>Year:<INPUT type="text" id="year"></P>
<INPUT type="button" value="OK" onClick="getAge();">
<HR>
<?php
if (isset($_GET["y"])) {
echo "Your age is ", date("Y") - $_GET["y"];
}
?>
</BODY>
<SCRIPT language="JavaScript">
function getAge() {
location.href = "JavaPHP.php?y=" + year.value;
}
</SCRIPT>
</HTML>
5
ADMTC-UCSC-University of Colombo
Basics of PHP - 1