Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Laern PHP Variables



Creating (Declaring) PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:


Example

$x = 5;
          $y = "John"
 

 


In the example above, the variable $x will hold the value 5, and the variable $y will hold the value "John".
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.



Think of variables as containers for storing data.


PHP Variables

A variable can have a short name (like $x and $y) or a more descriptive name ($age, $carname, $total_volume ).


Rules for PHP variables


Remember that PHP variable names are case-sensitive!


Output Variables

The PHP echo and print statements are used to output data

The following example will show how to output text and a variable:

         $txt = "q3schools.com";
          echo "I love $txt!";

The following example will produce the same output as the example above:

        $txt = "q3schools.com";
        echo "I love " . $txt . "!";

The following example will output the sum of two variables:

        $x = 5;
        $y = 4;
        echo $x + $y;

PHP is a Loosely Typed Language

In the example above, notice that we did not have to tell PHP which data type the variable is.

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.

Variable Types

In PHP, variables can hold different types of data. Here are some of the main variable types in PHP:


PHP supports the following data types:

Example

<?php
// Integer
$age = 30;
echo "Age: " . $age . "<br>";

// Float
$price = 19.99;
echo "Price: $" . $price . "<br>";

// String
$name = "John Doe";
echo "Name: " . $name . "<br>";

// Boolean
$is_valid = true;
echo "Is Valid: " . ($is_valid ? 'true' : 'false') . "<br>";

// Array
$colors = array("red", "green", "blue");
echo "Colors: " . implode(", ", $colors) . "<br>";

// Object
class Person {
public $name;
public $age;
}

$person = new Person();
$person->name = "John";
$person->age = 30;
echo "Person: " . $person->name . ", Age: " . $person->age . "<br>";

// NULL
$variable = null;
echo "Variable: " . var_export($variable, true) . "<br>";

// Resource (file handle)
$file_handle = fopen("example.txt", "r");
if ($file_handle) {
echo "File Handle: " . $file_handle . "<br>";
fclose($file_handle); // Close the file handle
}

// Callable (function)
function my_function($arg) {
echo "Hello, " . $arg . "<br>";
}

$callback = 'my_function';
$callback("World");

// Iterable (array)
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo "Number: " . $number . "<br>";
}
?>



Get the Type


To get the data type of a variable, use the var_dump() function.


Example


        $x = 5;
        var_dump($x);


See what var_dump() returns for other data types:


Example



        var_dump(5);
        var_dump("John");
        var_dump(3.14);
        var_dump(true);
        var_dump([2, 3, 56]);
        var_dump(NULL);


Assign String to a Variable


Assigning a string to a variable is done with the variable name followed by an equal sign and the string:


Example


        $x = "John";
        echo $x;

String variables can be declared either by using double or single quotes, but you should be aware of the differences.




Assign Multiple Values


You can assign the same value to multiple variables in one line:

Example

$x = $y = $z = "Fruit";


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

Zava

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

Zava