Objective. Students write PHP scripts that correctly declare variables, use all primitive data types, and apply arithmetic, comparison, and logical operators to produce expected output
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
PHP (PHP: Hypertext Preprocessor) is a widely used, open-source, server-side scripting language designed primarily for web development. Unlike HTML, which a browser renders directly, PHP code runs on the web server and sends finished HTML to the client. PHP files use the `.php` extension. Every PHP block opens with `<?php` and, when mixed with HTML, closes with `?>`.
A PHP statement ends with a semicolon (`;`). Missing semicolons are one of the most common beginner errors and will halt script execution with a parse error. Comments follow the same conventions as many C-style languages:
PHP is case-sensitive for variable names but case-insensitive for built-in function names and keywords.
In PHP every variable name starts with a dollar sign (`$`). Variable names must begin with a letter or underscore, followed by letters, digits, or underscores. They may not contain spaces or start with a digit.
```php
$billAmount = 45.00; // valid
$_taxRate = 0.08; // valid
$2fast = 10; // INVALID — starts with a digit
```
PHP is a loosely typed (dynamically typed) language: you do not declare a type when you create a variable. The interpreter determines the type from the value you assign, and that type can change during execution.
PHP has four scalar (primitive) data types:
| Type | Description | Example literal |
|------|-------------|----------------|
| `int` | Whole numbers, positive or negative | `42`, `-7` |
| `float` | Numbers with a decimal point (also called `double`) | `3.14`, `-0.5` |
| `string` | Text enclosed in single or double quotes | `'hello'`, `"world"` |
| `bool` | Logical true or false | `true`, `false` |
Two additional types you will encounter are `null` (no value) and arrays (covered in a later lesson).
Use `gettype($var)` or `var_dump($var)` to inspect a variable's current type and value — invaluable when debugging.
Type juggling is PHP's automatic conversion of a value from one type to another based on context. This is a powerful feature but also a common source of bugs.
```php
$a = "5"; // string
$b = 3; // int
$c = $a + $b; // PHP converts "5" to int 5; $c === 8 (int)
```
When a string that begins with a non-numeric character is added to a number, PHP converts it to `0`.
```php
$x = "hello" + 10; // $x === 10
```
Because type juggling can produce surprising results, always use strict comparison operators (see below) when the type matters.
| Operator | Meaning | Example | Result |
|----------|---------|---------|--------|
| `+` | Addition | `5 + 3` | `8` |
| `-` | Subtraction | `5 - 3` | `2` |
| `` | Multiplication | `5 3` | `15` |
| `/` | Division | `7 / 2` | `3.5` |
| `%` | Modulus (remainder) | `7 % 2` | `1` |
| `` | Exponentiation | `2 3` | `8` |
Integer division that produces a non-whole result automatically returns a `float`.
Comparison operators return a `bool`.
Best practice: prefer `===` and `!==` unless you specifically need loose comparison. Loose equality between `0` and `"foo"` evaluated to `true` in older PHP versions, a notorious source of security bugs.
| Operator | Meaning | Example |
|----------|---------|--------|
| `&&` or `and` | Both true | `$a && $b` |
| `\|\|` or `or` | At least one true | `$a \|\| $b` |
| `!` | Not | `!$a` |
Note that `&&` has higher precedence than `and`; prefer `&&` and `||` in most expressions to avoid precedence surprises.
The superglobal `$_GET` array holds values passed through the URL query string. For example, a request to `calc.php?bill=50&tip=18` makes `$_GET['bill']` equal to the string `'50'` and `$_GET['tip']` equal to `'18'`. Always cast or validate these values before arithmetic:
```php
$bill = (float) $_GET['bill'];
$tip = (float) $_GET['tip'];
```
Web programming does not involve physical electrical hazards the way hardware work does, but it has its own safety concerns:
These habits begin on day one — they are far easier to build than to retrofit.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.