DEV Community

loading...
Cover image for The basics of PHP — Part 1

The basics of PHP — Part 1

Tadeu Barbosa
I'm passionate with development.
・4 min read

Sometimes we learn a new programming language, but we go to the practical part and ignore the theoretical one. I like to share a bit of my learning about PHP language in some articles and this is the first part. I hope this help you!

image

Syntax

A PHP code is made of symbols and characters, called syntax.

Punctuation

Some punctuation symbols are used by programming languages in two ways: as a language operators, that cause an action when interpreted (e.g.: , ; . ). And to construct sentences.

Semicolons:

Semicolons are PHP code statement terminators. The code must end with a semicolon, which tells the parser to execute the statement. When the semicolon is missing, the parser throws a parser error, which indicates a syntax problem.

At each statement termination, the parser compiles (generates the bytecode and execute) before moving to the next line.

Opening, closing and shortcut tags

PHP uses opening and closing tags to tell the computer to use a PHP parser to interpret the following code, until a closing tag is encountered, when the parser stops.

The tags is: <?php, or <? (when short_open_tags directive is enabled or --- enable-short-tags are called). The tag: <?php echo, can be used as: <?=. Which results in the same response.

If a file contains only PHP code, a good practice is not to use the closing tag. If the file contains another kind of code, then the closing tag is required.

If a closing tag does not exist in a pure PHP file, and there is anything else after closing tags like a blank line or space, that blank line or space is sent back to a requesting cliente and certainly will cause a hard-to-debug bug.

Comments

Commenting code is helpful to clarify intent. PHP supports C, C++, and Unix shell-style comments.

A double slash (//) is interpreted as a beginning comment marker, this comment can appear anywhere in a line.

A multi line comment (/../) is also available.

It's not a good thing to use a lot of comments.

Operators

Operators form a significant portion of PHP code and inform the parser of actions to take.

Arithmetic

The standard arithmetic operators:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulus (remainder of a division)

Bitwise

Allow manipulation of a single bit within a byte.

Assignment

An assignment is a way to store a value in memory, and reference it with and identifier variable or constant.

Assing

A single equal operator is not a test for equality, it's an assignment operator.

$value = 123;
Enter fullscreen mode Exit fullscreen mode

Assignment short forms (combined operators)

PHP as a shorthand for arithmetic operations that developers can use. The format is: "[arithmetic operator]=". This executes and stores the result. The arithmetic operator can be any of: +-*/&|^>><<.

$value = 1;
$value += 3;
echo $value; // prints 4

// Or

$value = "Hello";
$value .= " world";
echo $value; // prints "Hello world"
Enter fullscreen mode Exit fullscreen mode

Increment/decrement (++ -- )

PHP supports C-style increment and decrement operators. The placement of the operator determines when the operation happens. Placing it before, the value will be incremented by one before evaluation. Placing it after, the value will be incremented after.

Value comparation

PHP supports several different options for comparing values.

Equality: A double equals operator (==) compares two values and returns true or false if they are equal.

Inequality: This one (!=; "bang equals") tests the inequality of two values.

Value/Type Equality and Inequality: the tripe equals (===) or the bang double equals (!==), does not attempt to coerce types but make the comparison based on both type and value;

Greater/Less Than:

PHP supports four greater/less than operators:

  • < Less than
  • > Greater than
  • <= Less than or equal to
  • >= Greater than or equal to

It's important to know that this: => , is used in creating arrays. If you attempt to use this one as Greater Than or Equals, PHP will error.

String comparison

In PHP, combine strings by using the concatenate operator (.). This joins two strings together.

echo "Hello " . "world!" // prints: Hello world!

// Additionally, you can use the shorthand operator (.=).
$hello = "Hello ";
$hello .= "world!";
echo $hello; // prints: Hello world!

// It's the equivalent to:
$hello = "Hello ";
$hello = $hello . "world!";
echo $hello; // prints: Hello world!
Enter fullscreen mode Exit fullscreen mode

Array

Array processing is one area where PHP shines above other languages. In addition to the many built-in functions to assist in processing arrays. These following operators can be used:

  • + Union
  • == Equal by value
  • === Identical, or equal by value and type
  • != Not equal by value
  • <> Not equal
  • !== Not identical by value or type

Example of union operator:

$arrayOne = ['a' => 'Apples', 'b' => 'Oranges', 'c' => 'Peaches'];
$arrayTwo = ['c' => 'Pears', 'd' => 'Bananas'];

print_r($arrayOne + $arrayTwo);
// prints: 
// [
//   'a' => 'Apples',
//   'b' => 'Oranges',
//   'c' => 'Peaches',
//   'd' => 'Bananas'
// ]

print_r($arrayTwo + $arrayOne);
// prints:
// [
//   'c' => 'Pears',
//   'd' => 'Bananas',
//   'a' => 'Apples',
//   'b' => 'Oranges'
// ]
Enter fullscreen mode Exit fullscreen mode

Note the results are dependent on which array is added to which. The left-hand array will prevail in case of duplicate keys.

The array equality and in-equality operators operate exactly as expected. They compare arrays and values, and return the appropriate response. Note that equals is not the same as identical.

For example:

$arayOne = ['apple', 'banana'];
$arayTwo = ['0' => 'apple', 1 => 'banana'];

var_dump($arrayOne == $arrayTwo);
// prints bool(true)

var_dump($arrayOne === $arrayTwo);
// prints bool(false)
Enter fullscreen mode Exit fullscreen mode

Although the values are the same, internally, the keys are different.

See you in the net part!

Discussion (0)