PHP Basics

📘 PHP 👁 38 views 📅 Dec 22, 2025
⏱ Estimated reading time: 1 min

PHP basics form the foundation for building dynamic web applications and are essential before moving to frameworks like Laravel. This section covers core PHP concepts with simple examples.


1. PHP Syntax

PHP code is written inside <?php ... ?> tags.

<?php echo "Hello, PHP!"; ?>

2. Variables

Variables start with $ and do not need type declarations.

$name = "Alice"; $age = 22;

3. Data Types

Common PHP data types:

  • String

  • Integer

  • Float

  • Boolean

  • Array

  • Object

  • NULL

Example:

$isActive = true; $price = 99.99;

4. Constants

Defined using define() or const.

define("SITE_NAME", "My Website");

5. Operators

Arithmetic Operators

$a = 10 + 5; $b = 10 * 2;

Comparison Operators

$a == $b; $a === $b;

6. Conditional Statements

if ($age >= 18) { echo "Adult"; } else { echo "Minor"; }

7. Loops

For Loop

for ($i = 1; $i <= 5; $i++) { echo $i; }

While Loop

while ($i <= 5) { $i++; }

8. Arrays

Indexed Array

$colors = ["Red", "Green", "Blue"];

Associative Array

$user = ["name" => "John", "email" => "john@example.com"];

9. Functions

function add($a, $b) { return $a + $b; }

10. Forms and User Input

Access form data:

$name = $_POST['name'];

11. Include and Require

include 'header.php'; require 'config.php';

12. Comments

// Single-line /* Multi-line */

Conclusion

Understanding PHP basics is crucial for creating dynamic web pages and working efficiently with Laravel. Mastering these concepts will make advanced PHP and framework features much easier to learn.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes