Skip to content

Latest commit

 

History

History
176 lines (122 loc) · 5.77 KB

n4.md

File metadata and controls

176 lines (122 loc) · 5.77 KB

Stars Badge Forks Badge Pull Requests Badge Issues Badge GitHub contributors Visitors

Don't forget to hit the ⭐ if you like this repo.

PHP Fundamentals

PHP (Hypertext Preprocessor) is a widely-used scripting language designed for web development. It is primarily used to create dynamic web pages and server-side applications. PHP is known for its simplicity, flexibility, and extensive support for interacting with databases and various web technologies.

Basic Syntax

PHP code is typically embedded within HTML code, enclosed between <?php and ?> tags. It can also be used within standalone PHP files with a .php extension.

Here's an example of a basic PHP script that displays "Hello, World!" on a web page:

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

In the above code:

  • The <?php tag denotes the beginning of PHP code.
  • The echo statement is used to output the specified text.
  • The text "Hello, World!" is enclosed within double quotes.
  • The ?> tag marks the end of PHP code.

1. Syntax and Variables

  • PHP code is embedded within HTML using opening and closing tags: <?php ... ?>.
  • Variables in PHP start with the $ sign and are case-sensitive.
  • Variable values can be assigned using the assignment operator (=).
  • PHP supports different data types, such as strings, integers, floats, booleans, arrays, and objects.

Example:

<?php
$name = "John Doe";
$age = 25;
$isStudent = true;

echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Is Student: " . $isStudent . "<br>";
?>

In the above code:

  • The $name variable stores the string "John".
  • The $age variable stores the integer value 25.
  • The . operator is used for concatenation.
  • The <br> tag inserts a line break in HTML.

2. Control Structures

  • PHP provides control structures like if...else, switch, while, for, foreach, etc., for implementing conditional and looping logic.
  • Conditional statements allow you to perform different actions based on different conditions.
  • Looping statements help you iterate over arrays, perform actions repeatedly, and control the flow of code execution.

Example:

<?php
    $score = 80;

    if ($score >= 90) {
        echo "Excellent!";
    } elseif ($score >= 75) {
        echo "Good!";
    } else {
        echo "Keep it up!";
    }
?>

In the above code:

  • The $score variable is set to 80.
  • The if statement checks if the score is greater than or equal to 90 and displays "Excellent!" if true.
  • The else if statement checks if the score is greater than or equal to 75 and displays "Good!" if true.
  • If none of the above conditions are met, the else block is executed, displaying "Keep it up!".

3. Functions

  • PHP allows you to define and use functions to group related code and reuse it throughout the program.
  • Functions can have parameters and return values.
  • You can create both built-in functions and user-defined functions.

Example:

<?php
function calculateSum($num1, $num2) {
    $sum = $num1 + $num2;
    return $sum;
}

$result = calculateSum(5, 7);
echo "Sum: " . $result;
?>

4. Arrays

  • Arrays in PHP can store multiple values in a single variable.
  • PHP supports indexed arrays, associative arrays, and multidimensional arrays.
  • Arrays can be accessed, modified, and manipulated using various array functions.

Example:

<?php
$fruits = array("Apple", "Banana", "Orange");

echo "First fruit: " . $fruits[0] . "<br>";

$student = array(
    "name" => "John Doe",
    "age" => 20,
    "grade" => "A"
);

echo "Student name: " . $student["name"] . "<br>";

$numbers = array(2, 4, 6, 8, 10);

echo "Count: " . count($numbers) . "<br>";
?>

5. File Handling

  • PHP provides functions for reading from and writing to files on the server.
  • You can open, close, read, write, and manipulate files using PHP's file handling functions.

Example:

<?php
$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);

$file = fopen("example.txt", "r");
$data = fread($file, filesize("example.txt"));
fclose($file);

echo $data;
?>

These are some of the fundamental concepts of PHP. With these foundations, you can start building dynamic web applications using PHP.

Note: Ensure that you have a web server with PHP installed to run PHP code.

Remember to save the code examples with a .php extension and run them on a web server to see the output.

Contribution 🛠️

Please create an Issue for any improvements, suggestions or errors in the content.

You can also contact me using Linkedin for any other queries or feedback.

Visitors