16
Basic Syntax and Variables
Disclaimer
This is the second post of a series, the main focus is my own growth on PHP. This isn't an informative article and what I say must be taken with a grain of salt. I'm always open for corrections and suggestions.
Being an embedded inside HTML, PHP has its own tags to mark the start and end of code to interpret, marked by the tags <?php
and ?>
respectively. This was the first thing that caught my attention, running functions inside the same file seemed so strange yet so easy to read. It is important to always finish a statement with a semicolon (;
).
<!DOCTYPE html>
<body>
<p>This will be ignored by the parser and will be printed as is.</p>
<?php
// This is a comment and won't affect the code.
echo 'This will be printed out!'
?>
</body>
According to PHP documentation, echo
is a language construct, a term that I researched about, but still would love to receive an explanation, that outputs expressions without additional newlines or spaces. This means that if you print something using it, the next echo will be in the same line without any space.
<?php
echo 'Lorem ipsum';
echo 'dolor sit amet';
?>
Will be outputted as Lorem ipsumdolor sit amet
.
Echo has a shortened method, by putting an equals sign instead of 'php' on <?php
. It would be the same as using <?php echo '' ?>
.
<?= 'Some random letters' ?>
In some situations, it might be necessary to use symbols such as quotes (" '
) or dollar signs without referencing variables or closing strings. You may use a backslash (\
) for this.
echo 'This isn\'t that hard.';
Variables in PHP are similar to other languages such as Python, though the way they are represented are a small nuisance for me. Variables must be prefixed with a dollar sign ($
) and are followed by a letter or an underscore, their names can be formed by any number and letter, also, they're case-sensitive. The variable also can't be named as $this
, as it refers to the object.
<?php
$name = 'Vinícius';
echo $name;
?>
The problem for me is that the dollar sign isn't present as a single character on the keyboard, and the keys that I need to press are pretty far from the common position of my hand.
This probably is just me being too fussy, and to say the truth doesn't even cause that big of a break in the flow of writing.
It's possible to use variables inside strings by just using double quotes (""
) instead of simple ones (''
), it is also recommended to use curly brackets ({}
) for better readability.
<?php
$name = 'Vinícius';
echo "My name is {$name}!";
?>
By default, variables are assigned by value, so they receive a copy of the value. You may assign by reference, meaning that whenever the base variable changes, the one that makes reference also changes.
<?php
$name = 'Vinícius';
$newName = &$name;
echo "My name is {$newName}! <br>";
$name = 'Ithalo';
echo "My name is {$newName}!";
?>
I couldn't really find possible ways of using assignment by reference without making my code harder to read and understand. Almost all the situations I could think of had better workarounds, though I can't really say as I didn't see how functions work exactly here.
- What are Language Constructs?
- In what situations assigning by reference is a good idea?
- Is there any tool similar to Codepen that works with PHP?
16