"PHP (Hypertext Preprocessor) is a case-insensitive and widely-used server-side scripting language designed for web development. It is mainly used for create dynamic and interactive websites and web pages. PHP can be embedded into HTML code and works on the server to generate content randomly and sent to the client's browser.
1. Server-Side Language: PHP scripts run on the server before the webpage is sent to the user.
2. Cross-Platform: PHP works on multiple platforms, including Windows, macOS, and Linux.
3. Database Integration: PHP can connect to databases like MySQL, PostgreSQL, and MongoDB, making it ideal for 4. developing data-driven websites.
5. Open Source: PHP is free to use and has a large community of developers.
6. Easy to Learn: PHP has a simple syntax that makes it easy for beginners, but it's powerful enough for professional developers.
7. Integration with HTML, CSS, and JavaScript: PHP can seamlessly integrate with HTML for front-end design and can be used alongside JavaScript for more interactive web experiences.
XAMPP is a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB (formerly MySQL), and interpreters for scripts written in PHP and Perl
1. Go to the official XAMPP website: https://www.apachefriends.org.
2. On the homepage, click the "Download" button.
3. Choose the version that matches your operating system (Windows, macOS, or Linux).
4. Select the appropriate version of XAMPP that you want to download (based on your PHP version preference).
1. Once the download is complete, locate the downloaded installer file and run it. 2. Windows: Right-click the installer and select "Run as Administrator." 3. macOS/Linux: You may need to give the installer permission to run by right-clicking and selecting "Open" or 4. using terminal commands.
Start Apache by Xampp Control Panel application from installed pc
create a file under installed xampp directory htdocs
after write some php code you can test it
Start Apache by Xampp Control Panel application from installed pc
If apache not run then change port from xampp\apache\conf\httpd.conf file
1. Variables Must Start with a Dollar Sign
- All PHP variables must begin with the dollar sign ($).
<?php
$name = "Sujit"; // Valid
?>
2. Variable Names Must Start with a Letter or Underscore (_)
- After the dollar sign, a variable name must start with a letter (a-z, A-Z) or an underscore (_)
- It cannot start with a number
<?php
$name = "John"; // Valid
$_name = "Doe"; // Valid
$1name = "Invalid"; // Invalid (cannot start with a number)
?>
3. Variable Names Can Contain Letters, Numbers, and Underscores
- After the first letter or underscore, variable names can contain letters, numbers (0-9), and underscores (_)
<?php
$var1 = 10; // Valid
$name_1 = "PHP"; // Valid
$name123 = "Code"; // Valid
?>
4. Variables are Case-Sensitive
- Variable names in PHP are case-sensitive. This means $name, $Name, and $NAME are considered different variables.
<?php
$name = "Sujit";
$Name = "Kumar";
echo $name; // Output: Sujit
echo $Name; // Output: Kumar
?>
5. No Spaces in Variable Names
- You cannot have spaces in variable names. If you need to separate words in a variable name, use an underscore (_) or camelCase (e.g., $myVariable).
<?php
$my_name = "John"; // Valid
$myName = "John"; // Valid
$my name = "John"; // Invalid (spaces not allowed)
?>
6. Predefined and Reserved Variable Names
- PHP has several predefined variables such as $_GET, $_POST, $_SERVER, etc. You should avoid using these names as regular variable names to prevent conflicts.
- Avoid using PHP reserved keywords (like class, function, public, etc.) as variable names.
<?php
$class = "SomeClass"; // Avoid using reserved keywords
?>
7. Variables are Dynamically Typed
- You don't need to declare the type of a variable in PHP. It is dynamically typed, meaning a variable can change its type based on the assigned value.
<?php
$x = 10; // Integer
$x = "Hello"; // String
?>
8. Variable Scope
- Variables can have local, global, and static scope.
- To access global variables inside a function, you need to use the global keyword or $GLOBALS array.
<?php
$x = 5; // Global scope
function myFunction() {
global $x; // Access global variable
echo $x;
}
myFunction(); // Output: 5
?>
you can find the php.ini file in directory and make sure display_errors = On. you can find directory php.ini using php function.
<?php
echo phpinfo();
?>
- Case Sensitivity: By default, constants are case-sensitive, but you can make them case-insensitive.
- Naming: Constant names do not need a $ symbol at the beginning, unlike regular variables.
- Global Scope: Constants are automatically global and can be accessed from anywhere in the script.
<?php
const name = "Sujit Tomar"; // This will create a constant variable.
$age = 21; // This will create a variable.
const __address = "Bangalore"; // This will create a constant variable.
define("email", "sujit@google.com"); // This will also create a constant variable.
const name = "Sujit Singh Rajput"; // This will throw an error because we can't change the value of constant.
age = 20; // This will throw an error because we can't create a variable without $ sign.
$age = 20; // This will change the value of variable. because variable can be changed.
email = "abc@gmail.com"; // This will throw an error because we can't change the value of constant.
echo "Name: " . name . "<br>";
echo "Age: " . $age . "<br>";
echo "Address: " . __address . "<br>";
echo "Email: " . email . "<br>";
?>
- Scope: const can only be used to declare constants inside the global scope or within classes, while define() can be used to declare constants anywhere.
- Constancy of Expression: const can only be used with scalar values (like strings, numbers, or booleans), while define() can take more complex expressions or even the result of function calls.
A string is a sequence of characters, like text.
<?php
$greeting = "Hello, World!";
echo $greeting; // Output: Hello, World!
?>
An integer is a non-decimal number, either positive or negative.
<?php
$number = 100;
echo $number; // Output: 100
?>
A float (also called double) is a number with a decimal point or in exponential form.
<?php
$pi = 3.1416;
echo $pi; // Output: 3.1416
?>
A boolean represents two possible states: TRUE or FALSE.
<?php
$is_logged_in = true;
$has_permission = false;
echo $is_logged_in; // Output: 1 (True is represented as 1)
echo $has_permission; // Output: (False does not print anything)
?>
An array is a data structure that can hold multiple values in a single variable. It can store values of any data type.
<?php
$cars = array("BMW", "Audi", "Mercedes", 500, 18.5);
echo $cars[0]; // Output: BMW
?>
An object is an instance of a class, which can store both data and functions. Objects are created from user-defined classes.
<?php
class Car {
public $make;
public $model;
public function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
public function showCar() {
return "Car: " . $this->make . " " . $this->model;
}
}
$myCar = new Car("BMW", "X5");
echo $myCar->showCar(); // Output: Car: BMW X5
?>
The NULL data type is used to represent a variable with no value. It can be assigned to any variable to clear its value.
<?php
$var = "Hello";
$var = NULL;
var_dump($var); // Output: NULL
?>
A resource is a special variable that holds a reference to an external resource, such as a file, database connection, or stream. You cannot create resources directly, but they are generated by functions like fopen()
<?php
$file = fopen("example.txt", "r");
var_dump($file); // Output: resource(5) of type (stream)
?>
PHP provides the gettype() function to check the type of a variable.
<?php
$age = 25;
echo gettype($age); // Output: integer
?>
PHP automatically converts data types as needed, which is known as type juggling. For example:
<?php
$sum = "5" + 10; // PHP converts the string "5" to an integer
echo $sum; // Output: 15
?>
php have two type of comments
// This is a single line comment
/*
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
*/
// Hi Thiis is demo text