BCA / B.Tech 13 min read

Introduction to PHP

Introduction to PHP in Hindi | Introduction to PHP: 


PHP (Hypertext Preprocessor) is a server-side scripting language, which is specially designed for web development. It was created by Rasmus Lerdorf in 1994. PHP is used to create dynamic web pages and web applications. It is an open-source and free language, which can be easily learned and used.

PHP code can be embedded in HTML, which makes it more useful and flexible. PHP supports various databases (such as MySQL, PostgreSQL, and SQLite).

Features of PHP in Hindi | Features of PHP :

  • Simple and easy to use: PHP's syntax is simple, which makes it easy to learn quickly.
  • Dynamic and Flexible: It helps in creating dynamic web pages.
  • Open-source: PHP is available for free.
  • Platform Independent: It works on Windows, Linux, Mac, etc.
  • Database Integration: PHP can connect to various types of databases.
  • Fast performance: It processes large amounts of data quickly.

Types in PHP in Hindi | Types in PHP: 

In PHP, data is mainly classified into different types. These data types help in working with PHP variables.

1. Scalar Types:

  • Integer: Whole numbers (e.g., 1, -5, 100).
  • Float: Decimal numbers (e.g., 3.14, -2.5).
  • String: Text data (e.g., "Hello", "PHP").
  • Boolean: Only true or false.

2. Compound Types:

  • Array: Stores more than one value in a single variable.
  • Object: Used for OOP (Object-Oriented Programming).

3. Special Types:

  • NULL: The variable has no value.
  • Resource: Used for external resources (such as database connections).
First Program in PHP in Hindi | First Program in PHP:

  • Use any text editor (like Notepad++, VS Code) to write PHP code.
  • Save the file with a .php extension (example: index.php).
  • Start the server (like XAMPP or WAMP).
  • Run the file in the browser.
Syntax:

<?php
// This is the first PHP program
echo "Hello, this is my first PHP program!";
?>


Output:

Hello, this is my first PHP program!




Basics of PHP :

PHP (Hypertext Preprocessor) is a server-side scripting language, which is specially used for web development. PHP is used to create dynamic web pages and web applications. It is an open-source language and can be easily integrated with HTML. The main purpose of PHP is to do processing on the server and send the required output to the user.

Basic Concepts of PHP

Syntax of PHP:

PHP code always starts with <?php and ends with ?>.

<?php
  echo "Hello world!";
?>

 Variables:

Variables in PHP start with $. They are dynamic, meaning they can store any type of data.

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

Data Types:

PHP mainly has the following data types:

String (e.g., "Hello")
Integer (e.g., 10, -5)
Float (e.g., 3.14)
Boolean (e.g., true, false)
Array (e.g., [1, 2, 3])
Object

 Operators:

PHP supports various types of operators:

Arithmetic Operators (+, -, *, /, %)
Assignment Operators (=, +=, -=)
Comparison Operators (==, !=, >, <)
Logical Operators (&&, ||, !)

 Conditional Statements:

In PHP, if, else if, and else statements are used to make decisions.

<?php
  $age = 20;
  if ($age > 18) {
    echo "You can vote.";
  } else {
    echo "You cannot vote.";
  }
?>

Loops:

Loops in PHP are used to simplify repeated tasks:

For Loop

<?php
  for ($i = 0; $i < 5; $i++) {
    echo "This is line number $i.<br>";
  }
?>
While Loop

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

 Functions:

Functions in PHP are used to reuse code.

<?php
  function greet($name) {
    return "Hello, $name!";
  }
  echo greet("Suresh");
?>

Form Handling:

PHP is used to process HTML form data. Example:

<form method="POST">
  Name: <input type="text" name="name">
  <button type="submit">Submit</button>
</form>

<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    echo "Hello, $name!";
  }
?>
 Files and Database Connection:

File Handling:

<?php
  $file = fopen("test.txt", "w");
  fwrite($file, "This is written by PHP.");
  fclose($file);
?>

Database Connection:

<?php
  $conn = new mysqli("localhost", "root", "", "my_database");
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }
  echo "Connection successful!";
?>
Advantages of PHP in Hindi | Advantages of PHP:

  • Easy and flexible language: PHP is easy to learn and understand.
  • Open-source: It can be used for free.
  • Multiplatform support: PHP runs on Windows, Linux, and MacOS.
  • Web-friendly: PHP is perfect for dynamic content and server-side processing.