PHP Strings

Strings in PHP are sequences of characters enclosed in single quotes (' ') or double quotes (" ").

Example:
<?php
    $name = "Samir";
    $greeting = 'Hello World';

    echo $name;
    echo $greeting;
?>
Common String Operations:
<?php
    $str = "Hello";

    echo strlen($str);      // Length of string
    echo strrev($str);      // Reverse string
    echo strtoupper($str);  // Convert to uppercase
    echo strtolower($str);  // Convert to lowercase
    echo str_replace("Hello", "Hi", $str); // String replace
?>
Concatenation:
<?php
    $first = "Hello";
    $second = "PHP";

    echo $first . " " . $second; // Using dot operator
?>

👉 Explanation:

  • Strings can be created using single or double quotes.
  • The dot operator (.) is used to join strings.
  • PHP provides many built-in string functions like strlen(), strrev(), strtoupper(), etc.

PHP Numbers

Numbers in PHP can be integers or floating-point values. PHP automatically detects the number type.

Example:
<?php
    $x = 10;        // Integer
    $y = 20.5;      // Float

    echo $x;
    echo $y;
?>
Basic Number Operations:
<?php
    $a = 10;
    $b = 3;

    echo $a + $b;   // Addition
    echo $a - $b;   // Subtraction
    echo $a * $b;   // Multiplication
    echo $a / $b;   // Division
    echo $a % $b;   // Modulus
?>
Number Checking Functions:
<?php
    $num1 = 100;
    $num2 = 20.55;

    var_dump(is_int($num1));   // Check integer
    var_dump(is_float($num2)); // Check float
    var_dump(is_numeric("50")); // True: numeric string
?>

👉 Explanation:

  • PHP supports integers and floating-point numbers.
  • Arithmetic operators are used for mathematical operations.
  • Functions like is_int(), is_float(), and is_numeric() help check number types.
PHP Tutorial