PHP increment operators are used to increment a variable’s value and decrement operators are used to decrement a variable’s value. PHP also supports pre- and post-increment and decrement operators allowing for the increment or decrement of the value by one.
Assignment operators are used to define and manipulate scalar variables and not arrays. Scalar variables have the same naming convention where the dollar symbol $ is prepend. Therefore arithmetic types are scalar types. Arrays are aggregate types.
PHP double quoted strings can format strings using defined variables.
Newline character \n causes a line break when using double quotes.
Concatenation character . and concatenation assignment is .= to combine two strings.
Sample Code<?php
$name = "Jake\nRoberts";
echo "Your name is $name"; // prints Your name is Jake and Roberts on next line
$name .= " Roberts"; // Appends to string via concatenation assignment
echo "My name is" . $name; // Concatenation of string
?>
Variables store information such as values or other variables.
PHP variables are declared using the dollar symbol “$” before the name. PHP variables can be reassigned values using the assignment operator “=”, making it possible to assign a new value.
PHP string data types can be concatenated using the concatenation operator “.” making it easy to join strings.
Variables cannot start with a number because they must start with an underscore or letter. The second character can contain only alphanumeric characters or underscores.
Variable names are case-sensitive.
Sample Code<?php
$name = 'John'; // variable of string type being declared and initialized
$age = 18; // variable of integer type being declared and initialized
$height = 5.3; // variable of double type being declared and initialized
echo $name . ' is ' . $height . "m tall\n"; // concatenating variables and strings
echo "$name is $age years old."; // interpolating variables to string
?>
Double forward slash sequence // will mark all text until a newline as a comment.
Single Line Comment Example
Sample Code<?php
// This is a single-line comment.
?>
C-Style/Block Comments
The sequence /* is used to declare the start of the comment block and the sequence */ is used to declare the end of comment. All text between the start and end sequences is interpreted as a comment, even if the text is otherwise valid PHP syntax. These are sometimes called “C-style” comments.
Sample Code<?php
/* A block comment with the symbol /*
Note that the compiler is not affected by the second /*
however, once the end-block-comment symbol is reached,
the comment ends.
*/
?>
Edward Ojambo has been programming in various languages since 1985. He works as a freelance programmer creating custom databases, web, desktop and mobile applications. Private one-on-one tutorials are offered for computer programming, website development, Linux, email servers, web servers and search engine optimization.
All content was created for educational purposes and is not affiliated with official PHP. The content was extracted from examples that Edward Ojambo uses during work-related activities.
The content is released under Creative Commons BY-SA 4.0, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.
Use the content present in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to OjamboShop.com.