DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Vinicius Ithalo
Vinicius Ithalo

Posted on

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.


Syntax

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>
Enter fullscreen mode Exit fullscreen mode

;

Echo

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';
?>
Enter fullscreen mode Exit fullscreen mode

Will be outputted as Lorem ipsumdolor sit amet.

Echo Shorthand tag

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' ?>
Enter fullscreen mode Exit fullscreen mode

Escaping

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.';
Enter fullscreen mode Exit fullscreen mode

;

Variables

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;
?>
Enter fullscreen mode Exit fullscreen mode

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.
Keyboard

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.

Variables Inside Text

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}!";
?>
Enter fullscreen mode Exit fullscreen mode

Value and Reference

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}!";
?>
Enter fullscreen mode Exit fullscreen mode

The result would be:
Reference Result

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.

;

Questions

  1. What are Language Constructs?
  2. In what situations assigning by reference is a good idea?
  3. Is there any tool similar to Codepen that works with PHP?

Top comments (0)

12 APIs That You Will Love

Free and easy to use APIs for your next project, learning a new technology, or building a new feature.