You are on page 1of 6

ADMTC-UCSC-University of Colombo

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.

Basically you can insert PHP script anywhere

IMPORTANT: The file must be saved with the extension of .php, not .html

3.3 Basic structures


A statement

semi-colon: same as JavaScript

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 {
...
}

IMPORTANT: Different from JavaScript (else if)

Note: Condition can be as follows.


A == B, A != B, A < B, A > B, A <= B, A >= B
Loop
while (condition) {
...
}

Same as JavaScript

for (initial_procedure; condition; procedure_for_every_loop) {


...
}

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

IMPORTANT: Do not use + for connecting string. You must use ..


2

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

4. Basic statement and functions


echo statement
Outputs text or variable onto the page. The most important function you will use in PHP!
IMPORTANT: If you dont output anything, then nothing will be shown on Web browser!
(Example 1)
<HTML>
<BODY>
<?php echo "<H1>Hello, PHP!</H1>"; ?>
</BODY>
</HTML>

You should output HTML tags

(Example 2)
<HTML><BODY>
<?php
variable
$age = 35;
echo "<H1>I am $age years old</H1>n";
?>
</BODY></HTML>

n means new line

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";

Some of the advanced formats for date() finction


echo date("F j, Y (l) h:i:s");

// See PHP help file for the detail

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.

Traditional array (just like in other programming languages)


$a[0] = "Hello, ";
$a[1] = "My name is ";
$a[2] = "UCSC";
echo "<P>", $a[0], $a[1], $a[3], "</P>";

Use square brackets [ ] to indicate


the index of data stored in an array

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

Associative array (the key is not a number, but a string)


$a["name"] = "UCSC";
$a["age"] = 41;
$a["office"] = "ADMTC";
echo "name: ", $a["name"], "<BR>";
echo "age: ", $a["age"], "<BR>";
echo "office: ", $a["office"], "<BR>";

You can mix string and number


for each item in an array

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> ";
}

6. Basic practical examples


6.1 Simple Access Counter
This example uses PHP functions regarding file access (open, read, write or close the file).
You need to create separate text file (count.txt) that contains only 1 character 0, then
upload the file onto the same directory as the following PHP file.
<HTML>
<BODY>
<?php
if ($f = fopen("count.txt", "r+")) {
// Open "count.txt" file
$count = fgets($f, 10);
// Read the number from the
file
$count = $count + 1;
// Increment the number
fseek($f, 0);
// Go to the beginning of file
fputs($f, $count);
// Write the new number
fclose($f);
// Close the file
}
?>
<p>You are the <?php echo $count; ?>-th guest to this page</p>
</BODY>
</HTML>

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

6.2 Getting parameter values by URL


Standard URL can contain parameters to a dynamic page by using ? character as follows.

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

6.4 Guestbook using HTML form


You need to create blank text file (messages.txt), then upload the file onto the same
directory as the following PHP file.
<HTML>
<BODY>
<H1>Guest Book</H1>
<P>Write a message and click button</P>
<FORM method="POST">
<INPUT type='text' name='message' size=50>
<INPUT type='submit' value='send'>
</FORM>
<HR>
<?php
// Write new message to a file
if (isset($_POST["message"])) {
if ($f = fopen("messages.txt", "a")) {
fputs($f, htmlspecialchars($_POST["message"]) . "n");
fclose($f);
}
htmlspecialchars()
}
function converts special characters to
// Read existing messages from a file
a safe notation for HTML display. You
if ($f = fopen("messages.txt", "r")) {
should use this function whenever you
display user-input strings on HTML
while ($m = fgets($f)) {
echo "<P>$m</P>";
}
fclose($f);
}
?>
</BODY>
</HTML>

You might also like