ITGS PHP Guide
Study this document until you understand & are able to use everything described here. If anything is unclear, ask immediately.
You can comment or ask questions at the bottom of this page. Go there now?
Prerequisites
or — What you should already know
Variables & conditions
<?php
$fruit = 'apple';
$count = 5;
echo $count . ' ' . $apple;
if ($count != 1) {
echo 's';
}
// Should display: "5 apples";
?>
What functions are, how to create and use them
<?php
function my_thing($what) {
echo 'I have: ' . $what;
}
my_thing('a car');
my_thing('an iPad');
?>
How to use arrays
<?php
$fruits = array('apple', 'tomato', 'potato');
print_r($fruits); // Prints a nicely formatted view of the array contents
echo 'Which does not belong? ';
foreach ($fruits AS $fruit) {
echo $fruit;
echo ' ';
}
$grades = array(
'Tom' => 5,
'George' => 4,
);
echo $grades['Tom']; // 5
?>
PHP and user-submitted data
or — How to communicate with the outside world
This is a form:
// HTML code:
<form action="index.php" method="get">
Nimi: <input type="text" name="my_name" />
<input type="submit" value="ENGAGE!" />
</form>
When the ENGAGE! button is pressed (that’s called submitting the form),
the browser is redirected to an address that looks like:
<form>-s "action"
+ ?
+ <input>-s "name"
= [WHAT THE USER TYPED]
.
With the above form, the address would be
index.php?my_name=Joel
Using the data on the "action" page (index.php
)
The $_GET
array is created automatically by PHP. It contains the data from
the query string (?my_name=Joel
).
<?php
print_r($_GET);
/*
Will display:
Array
(
[my_name] => Joel
)
*/
?>
To get a certain value, we access it like we would with any other array
<?php
echo $_GET['my_name']; // Displays: Joel
?>
A more complete, working example:
// This is index.php
<html>
<body>
<?php
// The isset() function checks if a variable exists
// We do this to only show the "Hello" text after the user submits the form.
// Without this check, the page would always display "Hello, "
if (isset($_GET['my_name'])) {
echo 'Hello, ' . $_GET['my_name']; // Displays somethign like "Hello, Joel"
}
?>
<form action="index.php" method="get">
Nimi: <input type="text" name="my_name" />
<input type="submit" value="ENGAGE!" />
</form>
</body>
</html>
Another example. Something related to a previous homework assignment.
<html>
<body>
<form action="index.php" method="get">
Enter grade: <input type="text" name="grade" />
<input type="submit" value="Motivate!" />
</form>
<table>
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
<tr>
<td>Mart</td>
<td>
<?php
if (isset($_GET['grade'])) {
$grade = intval( $_GET['grade'] );
/* The intval() function "returns the integer value of a variable"
This means that we can now be completely sure that $grade is a number,
even if the user entered "stupid" into the form above.
When you pass this function non-number values (e.g. "stupid"), it will
return 0 (zero).
*/
if ($grade == 0) {
echo '<b>0</b>';
} else if ($grade < 3) {
echo '<img src="http://up.elevantstudios.com/201103131953-paavo.jpg">';
} else {
echo $grade;
}
}
?>
</td>
</tr>
</table>
</body>
</html>
The code above does the following:
- Displays a form (with a single input, "grade")
- Displays a table with 2 columns - name & grade
- When the user enters a grade into the form input, and submits the form, the grade, (or a motivating picture) is shown in the appropriate table cell