- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Note: I am primarily using this as a study tool as I'm upskilling my foundational knowledge. With this in mind, this document is ongoing, and I'll likely be updating it weekly.
Opening and closing tags:
<?php ?>
Note: omitting the closing tag at the end of a file can be beneficial so you don't have any unwanted white space at the end of the file
Printing:
echo 'This prints to the screen.';
<?= 'This works too.'; ?>
Commenting:
One line:
# This is a one-line comment in PHP
Multi-line:
/* This is a multi-
line comment in PHP */
Checking variable type:
var_dump($var_name);
Comparing Floats:
Note: Comparing floats in PHP is tricky because of precision. The following is a workaround for that limitation.
<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if (abs($a - $b) < $epsilon) {
echo "true";
}
?>
String Definitions:
'this is a string'
"this is a string"
<<<END
this
is also
a string
END
<<<'EOD'
This is an example of
another type of string
you can use in PHP.
EOD
String Interpolation:
<?php
$juice = "apple";
echo "He drank some $juice juice." . PHP_EOL;
?>
or
echo 'this line prints a {$var}';
String concatenation uses a . rather than a + in PHP.
I used PHP's documentation as the source of my information here. Some of the code will be similar or identical.
Opening and closing tags:
<?php ?>
Note: omitting the closing tag at the end of a file can be beneficial so you don't have any unwanted white space at the end of the file
Printing:
echo 'This prints to the screen.';
<?= 'This works too.'; ?>
Commenting:
One line:
# This is a one-line comment in PHP
Multi-line:
/* This is a multi-
line comment in PHP */
Checking variable type:
var_dump($var_name);
Comparing Floats:
Note: Comparing floats in PHP is tricky because of precision. The following is a workaround for that limitation.
<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if (abs($a - $b) < $epsilon) {
echo "true";
}
?>
String Definitions:
'this is a string'
"this is a string"
<<<END
this
is also
a string
END
<<<'EOD'
This is an example of
another type of string
you can use in PHP.
EOD
String Interpolation:
<?php
$juice = "apple";
echo "He drank some $juice juice." . PHP_EOL;
?>
or
echo 'this line prints a {$var}';
String concatenation uses a . rather than a + in PHP.
I used PHP's documentation as the source of my information here. Some of the code will be similar or identical.