What is a php variable?
Computer stores data into a memory. In order to identify which data is stored where it uses a unique keyword which we call variable.
Variables basically store data. They are pointers to a memory location in a computer. Think about this way library is a computer and books are variables. Each book has a specific information.
How to define a variable in php?
In php language every variable is prefixed using $ symbol followed by a unique name. There are some rules to define a variable in php. If you dont follow the rule you will get syntax errors:
- A variable must start with $ symbol followed by a unique name
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha numeric string and underscore
- Variable names are case sensitive
Always write meaningful name to variable so that it is easily redable by other developers.
Note: PHP automatically converts the variable to the correct data type, depending on its value.
Variable scopes in php?
In php script, you can define variables anywhere. Scope is a part of a php script where you can use the defined variables. Followings are different types of scopes:
We will learn about these types later in this tutorial.
Types of php variables
Scalar Variables in php
Scarar variables incude following types of variables
What is a boolean variable in php?
Boolean variable basically determines true or false expression. Let's check following example:
<?php // declare a boolean variable $is_allowed_to_process = true; ?>
What is an integer variable in php?
Integer variables can be declared in decimal, hexadecimal, octal or binary notation and can be declared using + or - sign to define them positive or negative integers.
Followings are some of the ways you can declare an integer variable:
<?php // decimal number $number = 1234; // a negative number $negative_number = -123; // octal number (equivalent to 83 decimal) $octal_number = 0123; // hexadecimal number (equivalent to 26 decimal) $hexa_decimal_number = 0x1A; // binary number (equivalent to 255 decimal) $binary_number = 0b11111111; ?>
What is a float variable in php?
Floating point number variables are also known as floats, doubles or real numbers:
<?php $a = 1.454; $b = 1.2e5; $c = 8E-10; ?>
Note: Floating point numbers have limited precision. Do not compare floats for equality if your applications has to correctly do sum or any other math. Checkout Why don't my numbers add up?
What is a string variable in php?
A single character refers to a byte in a computer. Combination of bytes are called a string. String represents number of characters in a row.
Followings are some of the types of strings in php:
- single quoted
- double quoted
Let's check out some examples to see how they can be declared:
<?php /** SINGLE QUOTED STRINGS **/ $name_of_fruit = 'apple'; $name_of_city = 'Toronto'; /** DOUBLE QUOTED STRINGS **/ $what_is_php = "PHP is a simple programming language"; $html_string = "<h1>This is a html title tag example</h1>"; /** HERE DOC STRING **/ $html_page = <<<HTML <html> <body> <p>Sample Paragraph</p> </body> </html> HTML; /** NOW DOC STRING **/ $sample_string = <<<'EOD' Example of string spanning multiple lines using heredoc syntax. EOD; ?>
What is the difference between heredoc and nowdoc?
Followings are some of the difference between heredoc and nowdoc strings:
- Nowdocs are to single-quoted strings while heredocs are to double-quoted strings.
- Variables declared in nowdocs can not be escaped
- Variables declared in heredoc will be escaped with their values
Compond variables in php
Compound variables in php can include following types of variables:
Arrays in php
Arrays in php are basically ordered map. A map defines association between a key and value. Followings are some of the ways you can define arrays in php:
<?php // arrays with default index starts with 0 $name_of_fruites = ['mango', 'apple', 'grape']; // array with keys/values i.e. associative array $type_of_cars = [ 'mazda' => ['mazda3', 'mazda5'], 'toyota' => ['camry', 'raw4'], ]; ?>
We will learn more about other types of variable in a later tutorial. There are lot to learn about variables because you are beginners we have to consume php slowly.
Let's do some practise because practise will make us a good programmer. Let's do some excerise regarding variables in our next tutorial.
News and Updates
Subscribe to our newsletter and receive the latest news from TheThemeio.
- Remote Data
- Organizing AngularJS
- Github Pages
- What is AngularJS?
- Model and Controller
- Service Workers
- Symfony2 Social Login