PHP Syntax
Simple Tips for PHP Beginners
When you start PHP programming, you will get syntax errors. Many of them are small mistakes, but they can take a lot of time to find if you are not familiar with the basic syntax. These simple tips will save your time.
Habits that help
- Type the code yourself. Copy and paste is fast, but you will not learn the syntax. Typing builds the habit.
- Start small. Write and run a few lines at a time, so you know which line has the problem.
- Read the error message. It tells the file and the line number. The real mistake is often on the line above.
- Show errors on your local computer. Use
error_reporting(E_ALL);while learning. Read PHP Errors. - Use a good editor. Visual Studio Code highlights the code and shows missing brackets.
- Write comments and indent your code. It makes the code easy to read and to fix. See PHP Comments.
- Practice every day, even 30 minutes. Try the PHP exercises.
Common beginner mistakes
| Mistake | Example | Fix |
|---|---|---|
| Missing semicolon | echo "Hi" | End every statement with ; |
| Brackets or quotes not closed | echo "Hi; | Check that every (, { and quote has its pair |
| Wrong variable case | $Name and $name | Variable names are case-sensitive. See PHP Variables |
| Missing dollar sign | name = "Robin"; | Variables start with $ |
| Using = for comparison | if ($a = 5) | Use == or === to compare |
| Opening the file directly | Double click on the file | Run it with http://localhost. See PHP Installation |
Experiment with Basic PHP Syntax Errors
When you start PHP, you will see errors. They stop your program, and sometimes a small mistake takes a long time to find. A good way to get friendly with them is to make the mistakes yourself and see the message. Then you will know them when they come in real work.
A working program
<?php
echo "Hello World!";
?>
Output:
Hello World!
Now we will break this program in different ways. Run each example on your local computer and read the message. The file name and line number in your messages will be different.
Experiment 1: missing semicolon
<?php
echo "Hello"
echo " World";
?>
Parse error: syntax error, unexpected token “echo”, expecting “,” or “;” in /path/index.php on line 3
PHP reports the line after the missing semicolon, because it only understands the problem when it reads the next statement.
Experiment 2: undefined variable
<?php
$name = "Robin";
echo $nme; // typing mistake
?>
Warning: Undefined variable $nme in /path/index.php on line 3
This is only a warning, so the script goes on. The cause is a typing mistake, or the different case of the variable name.
Experiment 3: unknown function
<?php
printt("Hello");
?>
Fatal error: Uncaught Error: Call to undefined function printt()
Experiment 4: bracket not closed
<?php
if (true) {
echo "Yes";
?>
Parse error: Unclosed ‘{‘ on line 2
Experiment 5: divide by zero
<?php
echo 10 / 0;
?>
Fatal error: Uncaught DivisionByZeroError: Division by zero
What to learn from it
- Parse errors are syntax mistakes. The program does not start at all.
- Warnings and notices do not stop the program, but they show a problem to fix.
- Fatal errors stop the program in the middle.
See all types of errors in PHP Errors and the types of errors in PHP.
Using phpinfo() – Display PHP Configuration & Modules
phpinfo() is a built-in PHP function that shows a full page of information about your PHP: the version, the configuration file in use, the settings and the loaded modules (extensions).
When PHP is installed, many extensions come with it. Some are enabled and some are not. Extensions add features to PHP. For example, the date extension gives date and time functions, mysqli is used to connect with MySQL, and curl is used for APIs. It is good to look at them.
Example using phpinfo()
<?php
phpinfo();
?>
Save it as info.php in your project folder, and open it with http://localhost/info.php. You will see a long page with many tables.
What you can find on that page
- The PHP version and the system where it runs.
- The path of the loaded
php.inifile, so you know which file to edit. - Settings like
upload_max_filesizeandmax_execution_time. - The list of all loaded extensions, like mysqli, curl and mbstring.
Other ways to check
<?php
echo "PHP version: " . PHP_VERSION . "<br>";
var_dump(extension_loaded("mysqli"));
echo ini_get("upload_max_filesize");
?>
Output:
PHP version: 8.2.12
bool(true)
2M
PHP_VERSIONgives the version.extension_loaded("name")tells if an extension is on.ini_get()reads one setting.- In the command line,
php -v,php -m(modules) andphp -i(same as phpinfo) work.
Related: PHP Installation.