PHP - Lesson-1 Variables
Learn PHP Basics
Introduction
What is PHP? PHP (Hypertext Preprocessor) is an open-source server-side scripting language mainly used for web development. PHP code is executed on the server, and the result is sent to the browser as plain HTML.
Unlike HTML, PHP can:
- Connect to databases
- Handle forms
- Manage sessions
- Control user access
Example: Hello World
The simplest PHP script prints text to the browser. Here’s how you can write a "Hello World" program:
echo "Hello, World!";
?>
<?php and end with ?>.
Variables in PHP
Variables in PHP start with a dollar sign ($). They can store strings, numbers, or other data types.
$name = "Alice";
$age = 25;
echo "My name is $name and I am $age years old.";
?>
Data Types in PHP
Like every programming language, PHP also uses data types to define the kind of data a variable can hold. In many languages, understanding data types may feel difficult or unnecessary for beginners. However, PHP data types are easy to understand, and working with them is very simple because PHP is a dynamical written language.
In PHP, you do not need to declare a data type explicitly. The data type is automatically determined based on the value assigned to the variable. This feature makes PHP beginner-friendly and reduces complexity while coding.
$number = 10; // Integer
$price = 99.50; // Float
$text = "PHP"; // String
$status = true; // Boolean
;
?>