Appearance
Basic syntax
PODCAST
PHP script/code blocks
- Open course/hello_world.php (a variant of the example on the Introduction page, but now "embedded" in our project)
php
<!doctype html>
<html lang="en">
<head>
<?php include_once('../shared/meta.php') ?>
<title>Hello world</title>
</head>
<body>
<?php include_once('../shared/nav.php') ?>
<main>
<h1>Hello world</h1>
<p><?php echo 'Hello world'; ?></p>
</main>
<?php include_once('../shared/footer.php') ?>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- This file contains HTML tags and PHP script/code blocks
- A PHP script/code block starts with
<?php
and ends with?>
- Every statement ends with a semicolon
;
- The command
echo
is used to output text on the screen
REMARKS
- If a PHP file only contains one (large) PHP script, the closing
?>
may be omittedphp<?php ...
1
2 - If a PHP script contains only one statement, the trailing
;
may be omitted: both<?php echo 'Hello world' ?>
and<?php echo 'Hello world'; ?>
are correct - If a PHP script contains only one
echo
statement, you can use the shorthand<?=
to open the PHP script block and execute the echo, as in<?= 'Hello world' ?>
Comments
- Use
// ...
for single line comments or/* ... */
for multiple line comments
php
<?php
// This is a single line comment
/* This comment section
spans several
lines */
?>
1
2
3
4
5
6
7
2
3
4
5
6
7
- In PhpStorm you can use the shortcuts
Ctrl
+/
(single comment line) orCtrl
+Shift
+/
(multiple comment lines)
Variables
- Open course/variables.php
php
<article>
<?php
$student1 = 'John Doe';
$percentage1 = 73.08;
$student2 = 'Jane Smith';
$percentage2 = 64.84;
echo '<p>' . $student1 . ' graduates with ' . $percentage1 . ' %</p>' . "\n";
echo "<p>$student2 graduates with $percentage2 %</p>\n";
?>
</article>
<article>
<?php
echo <<<RESULTS
<p> $student1 graduates with $percentage1 %</p>
<p> $student2 graduates with $percentage2 %</p>
RESULTS;
?>
</article>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NAMING CONVENTIONS
- All variables always start with a dollar sign
$
, followed by the name of the variable - Variable names start with a letter or
_
(not a number!) and contain only alpha-numeric characters and_
(no spaces!) - Variable names are case-sensitive
- Most PHP programmers use the camelCase or snake_case notation for variable names
- Variables don't need to be declared (or given a data type) before giving them a value
- The most important data types in PHP are
- numbers
5
,3.14
,34e-2
(= 0.34), ...- without quotes
- strings
'John Doe'
,"John"
, ...- enclosed in single (
'...'
) or double quotes ("..."
)
- boolean values
true
orfalse
null
- for variables without a value
- numbers
Concatenation and interpolation
- The concatenation operator
.
"concatenates" multiple strings and variables, as in'<p>' . $student1 . ' graduates with ' . $percentage1 . ' %</p>'
- Don't forget to add an extra space before/after a variable for a readable result
- If you use double quotes (
"<p>$student2 graduates with $percentage2 %</p>"
), the variables can be included in a single string as they are automatically interpolated
TIP
The HTML code that is generated by PHP is not formatted (no line breaks are inserted). To keep the HTML source code readable, it is advisable to add a newline character (\n
) once in a while. This newline character should always be enclosed in double quotes ("...\n"
) .
Heredoc
- A third syntax for strings is called the heredoc notation
- Very handy string syntax
- No quotes and no
"\n"
are needed - Variables can be included/are interpolated
- Emmet can be used
- No quotes and no
- Start with the
<<<
operator, after which an identifier (e.g.RESULTS
) is provided - The "string" itself starts on a new line
- The identifier is repeated (on a new line) to close the string
- Very handy string syntax
php
echo <<<RESULTS
<p> $student1 graduates with $percentage1 %</p>
<p> $student2 graduates with $percentage2 %</p>
RESULTS;
1
2
3
4
2
3
4