PHP String Functions – Complete Guide for Beginners (with Examples)
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how...
Learn the basics of PHP with an introduction of variables, data types, and operators to create dynamic web page or web application .
When you start to learn PHP, one of the most important thing is to learn about Variables, data types and operators. In this tutorial we will discuss about how to create or define variables to create dynamic web page or web application. And also learn about data types and different types of operators and usage of it to create dynamic web application.
A variable is a container that stores data which can be used and manipulated throughout your program.
In PHP, all variables must be starts with a dollar sign ($).
let's we discuss about basic rules to create variables in PHP,
A valid variable name must be starts with a letter or underscore.
<?php
$var = 'Hello';
$Var = "World";
echo "{$var}, {$Var}"; // Outputs "Hello, World"
$1var = "Hello"; // invalid, starts with a number
$_1var = "World"; // valid, starts with an underscore.
?>
The data type defines, which type of data is stored in variables, whether it is string, integer or boolean or NULL. In PHP, there is no need to declare the data type whenever you create a variable - PHP determines it automatically.
Let's See, Which main data type is supported by PHP?
<?php
$str = 'This is String';
?>
<?php
$float_num = 1250.50;
?>
<?php
$is_logged_in = true;
?>
<?php
$fruits= array("Apple","Mango","Banana","Orange");
?>
<?php
$data= null;
?>
Operators are symbols that are used to perform operations on variables and values. They allow you to carry out tasks like mathematical calculations, comparisons, assignments, logical decisions, string handling, and working with arrays. In simple words, operators act like tools that help you manipulate data and control how your program behaves.
<?php
$a = 10;
$b = 5;
echo $a + $b; // 15
echo $a - $b; // 5
echo $a * $b; // 50
echo $a / $b; // 2
echo $a % $b; // 0 (remainder)
?>
<?php
$x = 10;
$x += 5; // 15
$x -= 3; // 12
?>
<?php
$a = 10;
$b = 20;
var_dump($a == $b); // false
var_dump($a != $b); // true
var_dump($a < $b); // true
?>
<?php
$age = 25;
$country = "India";
if ($age > 18 && $country == "India") {
echo "Eligible to vote";
}
?>
<?php
$x = 5;
echo ++$x; // 6 (pre-increment)
echo $x--; // 6 (then decreases to 5)
?>
<?php
$a = "Hello";
$b = " World";
echo $a . $b; // Hello World
$str = "Hello";
$str .= " World";
echo $str; // Hello World
?>
<?php
$a = ["a" => "red", "b" => "green"];
$b = ["c" => "blue", "d" => "yellow"];
print_r($a + $b); // Union of $a and $b
?>
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how...
Control structures in PHP allow developers to control the flow of code execution. In This tutorial,...
Learn the basics of PHP with an introduction of variables, data types, and operators to create dynam...
Basic introduction about what is php, benifits of php and how it runs on server and why it's widely...