🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Variables

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
?>
<?php // Type Some Code ?>

Comments

Single Line Comments

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.
*/
?>
<?php // Type Some Code ?>

Syntax

PHP looks for opening tag <?php and closing tag ?> in order to determine when to start and stop interpreting code.

PHP can also use the short echo tag <?= as short-hard for <? echo. Short tags must be enabled in the php.ini configuration or during the PHP build.

The closing tag can be eliminated if a file only contains PHP code.

Hello World Example

Sample Code<?php echo "Hello"; ?>

Hello World Short Tags Example

Sample Code<?='Hello, World!'; ?>

IDE

<?php // Type Some Code ?>

About

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.

Basic Data Types

String is a sequence of characters inside double or single quotes.

Integer is a non-decimal whole number and can be either positive or negative.

Float for floating point numbers also called double with a decimal point.

Boolean can only be TRUE or FALSE.

Array used to store multiple values in a single variable.

Object is an instance of a class which is a template for objects.

Null has no assigned value.

Resource stores a reference to functions and resources external to PHP such as a database call.

Sample Code<?php
$my_string = "Hello world!";
$my_string2 = 'Hello world!';
$my_integer = 2024;
$my_float = 2024.12;
$my_boolean = TRUE;
$my_string_array = ["One", "Two", "Three"];
class Greeting {
  public $name;
  public $age;
  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
  public function message() {
    return ‘My name is a ‘ . $this->name . ‘ and I am ‘ . $this->age . ‘ years old!’;
  }
}
$my_greeting_object = new Car(“John”, 23);
// Display Output Of Variables
echo $my_string;
echo $my_string2;
echo $my_integer;
echo $my_float;
echo $my_boolean;
var_dump($my_string_array);
echo $my_greeting_object;
?>
<?php // Type Some Code ?>

History

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.

Table : Recommended PHP Versions

VersionReleasedSupport EndSecurity End
8.22022-12-082024-12-082025-12-08
8.32023-11-232025-11-232026-11-23

Chapter 2: Data Types

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.

Table : Data Types

TypeDescriptionExample
StringStores a sequence of characters$name = “Jake”;
IntegerStores a non-decimal number$age = 24;
FloatStores a decimal point number$amount = 10.25;
BooleanStores either TRUE or FALSE$is_available = TRUE;
ArrayStores multiple values$names = [“Roy”, “Joe”, “Ben”];
ObjectStores instance of a class$person = new Person($name, $age);
NULLStores only NULL value$name = NULL;
UnsetConverts data type to NULL$age = (unset)$name;

Chapter 1: Getting started with PHP

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.

Requirements:

  1. Optional text editor for programming recommended
  2. Optional PHP installed and configured

Learning PHP

PROGRAMMING eBook LEARNING PHP OjamboShop.com eBook created by
Edward Ojambo