Coding/Learn/php/php strings numbers and math
PHP Foundational Course
01.Introduction, Server-Side Architecture & Syntax
02.Variables, Strict Types & Dynamic Casting
03.Strings, Numbers & Built-in Math Functions
04.Operators, Null Coalescing & Magic Constants
05.Control Flow: If-Else, Switch & Modern Match Expressions
06.Loops: While, For & Foreach Traversal
07.Functions, Type Declarations & Named Arguments
08.Arrays & Functional Array Manipulation
09.Superglobals, Form Handling & Input Sanitization
10.Date, Time & DateTimeImmutable Objects
11.File System, Streams & I/O Operations
12.Cookies, Sessions & State Management
13.Exceptions, Throwables & Custom Error Fences
14.Classes, Objects & Constructor Property Promotion
15.Inheritance, Access Modifiers & Method Overriding
16.Interfaces, Abstract Classes & Polymorphism
17.Traits, Static Members & Namespaces
18.PDO, MySQL & Prepared Statements against SQL Injection
19.JSON & XML REST API Interoperability
20.Capstone: Production Secure Backend API & User Registry Engine
Course Certificate0%

Complete all lessons to earn

LearnStrings, Numbers & Built-in Math Functions
3 / 20 0%
Lesson 03 of 20
8 mins read

Strings, Numbers & Built-in Math Functions

PreviousNext Lesson
In Plain English

Master PHP text manipulation and numeric computation. Learn the difference between single and double quotes, string length (strlen), substring extraction (substr), replacement (str_replace), and math utilities (round, min, max, rand).

The Mental Model

"Single quotes are a camera snapshot of pure text exactly as typed. Double quotes are an interactive display screen that renders variable values right inside the message."

Deep Dive: How It Works

String Concatenation: The dot (.) operator joins strings (e.g. $a . " " . $b).

Variable Interpolation: "User: {$user->name}" evaluates variables inside double quotes.

Core String Functions: strlen(), strpos(), strtoupper(), strtolower(), trim(), and str_replace().

Math Helpers: abs(), round(), ceil(), floor(), min(), max(), and random_int().

Syntax Blueprint

<?php

$greeting = "Hello, {$name}!";
$clean = trim($input);
$rounded = round(3.14159, 2);

Interpolate variables inside double quotes and call standard string/math functions.

PointCore Rules to Remember

Point star
Dot Operator: PHP uses the dot (.) operator for string concatenation, preserving + exclusively for addition.
Point star
Secure Randomness: Use random_int() instead of rand() for cryptographically secure integers.
Point star
Multibyte Support: Use mb_strlen() and mb_substr() when dealing with UTF-8 international characters.

Common Beginner Traps & How to Fix Them

Mistake:Using + to join strings ($msg = "Hello " + "World";).

Why it happens: + is strictly an arithmetic operator; it attempts to cast strings to 0 and adds them.

How to fix: Always use . for concatenation ($msg = "Hello " . "World";).

Pro Tip: Use Heredoc syntax (<<<EOD ... EOD;) for multi-line formatted strings and HTML templates.

Live Interactive Example

Hit Run Code to see it live
String Interpolation & Math Computations
PHP 8.2 / CLI
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: strtoupper uppercased the app name, round rounded 3.847 to 3.8, and min found 4.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Format Product Summary

  • PointDeclare $title = " cloud runner " and $cost = 19.456.
  • PointTrim $title and convert it to uppercase.
  • PointRound $cost to 2 decimal places.
  • PointPrint "Product: [TITLE] - $[COST]".
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates