Server Side Scripting using PHP
Server Side Scripting using PHP tests how a server generates HTML before sending it to the browser. Expect syntax correction, form handling, superglobal use, and the difference between client-side and server-side execution.
Core rules and concepts
- PHP runs on the server; the browser receives only the output, never the PHP source.
- Variables begin with
$and are loosely typed; strings concatenate with., not+. echoandprintsend output; each statement ends with;.- Forms:
method='post'sends data in the request body, read with$_POST;method='get'appends data to the URL, read with$_GET. isset()checks whether a form field was submitted before you use it.includegives a warning if a file is missing;requirestops execution.- Database access commonly uses
mysqli; sanitize input or use prepared statements to reduce SQL injection.
Worked example
Form: <form method='post' action='greet.php'><input name='name'><input type='submit'></form>
In greet.php:
<?php if(isset($_POST['name'])){ $n = htmlspecialchars($_POST['name']); echo 'Hello, ' . $n; } ?>
Reasoning: isset prevents an undefined-index warning on first load. $_POST['name'] matches the input's name attribute. htmlspecialchars neutralizes HTML characters before output. The dot joins the literal text to the variable.
Traps that cost marks
- Using
$_GETto read a form sent withmethod='post'. - Forgetting the
$sigil or the terminating semicolon. - Concatenating with
+, which gives a wrong result or numeric warning. - Believing users can see PHP code through 'View Source'.
- Using
includewhere a missing critical file must halt the script.