How to learn PHP from Scratch.

blog img

PHP is a widely-used server-side scripting language designed for web development but also used as a general-purpose programming language. PHP scripts are executed on the server, and the result is sent to the browser as plain HTML. Here are the basic concepts:

1. PHP Tags:

PHP code is embedded in HTML using specific opening and closing tags:

PHP

<?php
  // PHP code goes here
?>

Anything between <?php and ?> is treated as PHP code. This allows PHP to be mixed with HTML.

Example of mixing HTML and PHP:

PHP

<html>
<body>
  <h1>Welcome to my website</h1>
  <?php
    echo "Hello, World!";
  ?>
</body>
</html>

2. PHP Variables:

Variables in PHP start with a $ symbol. PHP automatically converts the variable’s type based on its value.

Example:

PHP

<?php
  $name = "Raghu";
  $age = 30;
  echo "My name is $name and I am $age years old.";
?>

PHP Output

  My name is Raghu and I am 30 years old.

3. PHP Data Types:

PHP supports several data types, including:

  • String: Text data, enclosed in quotes (" " or ' ').
  • Integer: Whole numbers (e.g., 42).
  • Float: Decimal numbers (e.g., 3.14).
  • Boolean: Either true or false.
  • Array: A collection of values, indexed or associative.
  • Object: Represents instances of classes.
  • NULL: A special type that represents a variable with no value.

4. Basic Syntax:

  • Echo: Used to output data.

PHP

<?php
 echo "Hello, World!";
?>
  • Comments: Comments can be single-line or multi-line

PHP

<?php
// This is a single-line comment
/* This is a multi-line comment */
?>

5. Control Structures:

  • If-Else Statements: Basic conditional logic.

PHP

<?php
  $age = 18;
     if ($age >= 18) {
         echo "You are an adult.";
     } else {
         echo "You are a minor.";
     }
?>
  • Loops: Used for repetitive tasks.

For Loop:

PHP

<?php
  for ($i = 0; $i < 5; $i++) {
    echo "Number: $i<br>";
  }
?>

While Loop:

PHP

<?php
  $i = 0;
  while ($i < 5) {
    echo "Number: $i<?br>";
    $i++;
  }
?>

6. Functions:

Functions allow you to reuse code. You can define your own functions in PHP.

Example:

PHP

<?php
  function greet($name) {
    return "Hello, " . $name;
  }

  echo greet("Raghu");
?>

7. Arrays:

Indexed Arrays:

PHP

<?php
 $fruits = array("Apple", "Banana", "Orange");
  echo $fruits[1]; // Outputs: Banana
?>

Associative Arrays:

PHP

<?php
 $person = array("name" => "John", "age" => 30);
  echo $person['name']; // Outputs: John
?>

8. Form Handling:

PHP is often used to collect form data. The $_POST and $_GET superglobals are used to retrieve form input.

Example:

HTML

<form method="post" action="process.php">
  Name: <input type="text" name="name">
  <input type="submit">
</form>

In process.php:

PHP

<?php
  $name = $_POST['name'];
  echo "Hello, $name";
?>

9. PHP and MySQL:

PHP can be used to connect to databases like MySQL. Here’s a basic example of connecting to a MySQL database and retrieving data:

PHP

<?php
   $conn = new mysqli("localhost", "username", "password", "database");

  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  $sql = "SELECT * FROM users";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
      echo "Name: " . $row["name"] . "
"; } } else { echo "0 results"; } $conn->close(); ?>

10. Error Handling:

PHP provides several ways to handle errors. The die() function is often used to stop script execution if something goes wrong.

Example:

PHP

<?php
   $file = fopen("test.txt", "r") or die("Unable to open file!");
  // Do something with the file
?>

Conclusion:

PHP is a flexible and powerful language for building web applications. By mixing PHP with HTML, you can dynamically generate content and interact with databases, making your website interactive.

Share your thoughts

Your email address will not be published. All fields are required.

Related post

  1. How to expert in PHP array

    An array in PHP is a data structure that allows…

  1. How to start PHP – in easy way

    PHP (Hypertext Preprocessor) is a widely-used open-source scripting language designed…

Popular post

  1. Eclipse IDE – Create New Java Project.

    Opening the New Java Project…

  1. How to start the project in android studio

    Android Studio Open the Android…

  1. How to use ACOSH function in excel

    The ACOSH function returns the…

  1. Complete Header tags in html – easy to learn

    H tags can be used…

  1. Best features in Python programme – easy to learn

    Python is the most widely…