php data types

Varible

Variable is a value that can store different types of data. Variables are highly useful in situations where we have it's assigned value used in multiple command lines or mutiple pages and then we need to change them occassionally. Let's say we are working on billing section of a department store where we need to add 13% vat in all customer's purchase. If we didn't create a variable, we need to state each customer's total purchases and then add the vat charges and calculate the total bill amount. But, if we created a system that takes the customer's purchase as a variable and calculates the vat charges and then sums up for total, it'd be a lot more easier.

John - purchase 1500
total cost  = 1500 + 13% of 1500
Joe - purchase 3120
total cost  = 3120 + 13% of 3120
if we created a variable for purchase that would be:
	$a = total purchase;
	$vat = 13% of $a;
	$total = $a + $vat
so everytime we entered the total parchase amount the total cost will be derived easily.

Naming Conventions in php

Variable name should always start with a $ symbol followed by letter or underscore not a number.

Space,*,- should not be used in variable.

$my variable is not a valid variable either $my_variable or $myvariable should be used. A better idea would be to use camel case in case of two words like $myVariable.

php reserved words or keywords should not be used while coding. though $echo is a valid variable, any developer might get confused if $echo and echo(); are used together.

Example case 1

$a = 10;
$b = 20;
$c = 30;

Three variables are created with different values. if i need to print the value of b i'll use

echo $b;

Example case 2

$a = 10;
$b = 20;
$c = 30;

$b = 40;
echo $b;

The pattern of code execution in php is top to bottom. Initially, the value of $b is stored as 20 but before executing the echo command $b's value is changed to 40. So, while printing the value $b will display the latest value. since it's a variable, it's value can change at anytime.

Reference

Reference in php means two variables pointing at same memory location.

Example 1

$a = 10;
$b = 20;
$c = 30;

$a  = $b;
echo $a; 

Result:
a = 20

Example 2

$a = 10;
$b = 20;
$c = 30;

$a  = $b;
$b = 100;
echo $a; 

Result:
a = 20 as the value of b will be 100 only after execution of echo function.

Example 3

 
$a = 10;
$b = 20;
$c = 30;

$a  = &$b; //$a and $b points at same memory location (reference)
$b = 100;
echo $a; 

Result: a = 100
It makes the value of $a change to $b even after the value of $a is pointed to $b. It can be useful while creating functions in future projects.

Data type can be checked using php function gettype(). Php supports following data types.

1. String

Any characters that appear between the double quotes or single quotes is string in php.

$a = 5; integer
$a = "5"; string
echo gettype($a);

Use of Single and Double Quotes

Generally, use of single quotes is much preferred by developers but there are cases where we must to use double quotes and cases where we must use single quotes as shown in the example below.

Incorrect - $a = 'ram's house'; Result: $a = ram

Correct - $a = "ram's house"; Result: $a = ram's house

Incorrect - $a = "they said "we are here""; Result: $a = they said

Correct - $a = 'they said "we are here"'; Result: $a = they said "we are here"

In the first example, we have got three single quotes. Php assumes the second single quote as the end of the string and ignores rest of the characters.

In the second example, string characters are wrapped by double quotes, hence, single quote in the middle is assumed as a character.

In the third example, there are 4 double quotes. Php assumes the second double quote as the end of the string and ignores rest of the characters.

In the fourth example, string characters are wrapped by single quotes, hence, double quotes in the middle is assumed as characters.

Escape Sequence

Escape sequence '/' can also be used to nullify those quotes that come in the middle of the string.

$a = "Ram said \"it's beautiful\"";

is_string()

is_string() function can be used to check if a data is string or not. The result will be either true or false.

echo is_string($a);

var_dump()

var_dump() function can also be used to return true or false while checking a value.

2. Numeric

Php recognizes numeric value even with double quotes that makes it possible to carry out arithmetic operations in php even with a string numeric. Numeric variable can further be classified into two categories.

1. Integer

2. Float

We can check if a data is integer or not by using is_numeric() function of php.

$a = 1234;
echo is_numeric($a);

2.1 Integer

Any numeric data either positive or negative but without decimal is integer. We can check if a number is integer or not by using is_integer() function of php.

$a = 10;	
echo is_integer($a);

2.2 Float

Any numeric data with a decimal is float. We can check if a number is integer or not by using is_integer() function of php.

$a = 10.34;	
echo is_float($a);

3. Null & Empty

Any variable with a value equivalent to 0 or is empty in php is termed as empty or false. Any variable with no value defined in php is null.

$a = '';

$b;

In this example, $a is empty while $b is null.

4. Boolean

A Boolean data has two possible values i.e. TRUE or FALSE. Booleans are often used in conditional testing.

$x = true;
$y = false;

5. Array

An array stores multiple values in a single variable. In the following example $books is an array. The PHP var_dump() function returns the data type and value. We can use print_r() function to see all the values in an array. we'll go through array in detail in latter lessons.

<?php 
$books = array("English","Nepali","Others");
var_dump($books);
print_r($books);
?>  

6. Object

An object stores data along with information on how to process that data in php. First we must declare a class of object. A class is a structure that contains properties and methods. We'll go through object in details in latter lessons.

<?php
class readSomething {
	function Books() {
		$language = "English";
		echo $language;
	}
}

// create an object
$Literature = new readSomething();

// show object properties
echo $Literature->Books();
?>  

0 Like 0 Dislike 1 Comment Share


Deepak

10 Aug, 2024

Nice tutorial. For more detail, read this tutorial. <a href = "https://www.scientecheasy.com/2024/08/data-types-in-php.html/">Data types in PHP</a>

Reply


Leave a comment