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.
PHP is a popular general-purpose scripting language that is maintained by The PHP Group and is harnessed for web development. It is released under the open source PHP License and its official website is https://www.php.net.
PHP can run on the command line or processed on a web server by an interpreter. As of 2024, PHP releases a minor release that is supported for 2 years and an additional year on only security fixes for a three year life cycle.
A data type is a grouping of data values. Variables are created to store a data type. In PHP, the assigning of a value will indicate the data type. PHP does not require a format specific in order to display the value.
A data type can be changed by assigning a different value. It is also possible to change the data type by casting. To cast a NULL data type, unset must be used.
In this chapter, you will learn about the brief history of PHP, its syntax, comments and string formatting. You can use your favourite text editor, IDE or use the built-in IDE. The built-in IDE can also be used to compile your code.
If you use your own text editor or IDE, you will need to also install PHP in order for your code to be interpreted. The built-in IDE has syntax highlighting using different colours for the keywords, comments, control-flow statements, variables and other elements.
To use the built-in IDE simple type the code in the IDE and click the compile button to display the results in the output area. Otherwise follow the requirements for the text editor and PHP.