Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

PHP Basic commands with examples

1. Variables

Store data like numbers, text, or arrays using $variable_name:

  • $age = 25; (integer)
  • $name = “EasyExamNotes”; (string)
  • $fruits = array(“apple”, “banana”, “orange”); (array)

2. Output

Use echo or print to display information on the screen:

  • echo $age; // Outputs 25
  • echo “Hello, $name!”; // Outputs “Hello, EasyExamNotes!”
  • print_r($fruits); // Prints the array contents

3. Arithmetic Operators

Perform calculations with mathematical symbols:

  • $sum = $age + 10; // $sum now equals 35
  • $average = ($age + 30) / 2; // $average gets 27.5
  • $remainder = $sum % 3; // $remainder becomes 2 (modulus)

4. Comparison Operators

Evaluate conditions using relational operators:

  • if ($age > 18) { echo “You are an adult.”; }
  • if ($fruit == “orange”) { echo “Good choice!”; }
  • if ($sum != 30) { echo “Calculation error!”; }

5. Loops

Repeat code blocks with while, do-while, or for loops:

  • while ($i < 5) { echo “$i. I am looping! “; $i++; }
  • do { echo “Counting down… $i”; $i–; } while ($i > 0);
  • for ($j = 0; $j < count($fruits); $j++) { echo $fruits[$j] . ” “; }

6. Functions

Define reusable code blocks for modularity:

  • function greet($name) { echo “Welcome, $name!”; }
  • greet(“Jane”); // Outputs “Welcome, Jane!”

7. Forms

Capture user input with HTML forms and access it in PHP:

  • <input type="text" name="username">
  • $username = $_POST[“username”]; // Get entered username in PHP