4

Basic Syntax and Variables

 2 years ago
source link: https://dev.to/vinic/basic-syntax-and-variables-14np
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

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.

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:

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?

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK