Variables and common data types in PHP

Tram Ho

1 . Out

In PHP, to declare a variable we will write a dollar sign ($) before it, and the data will be declared after the equal sign (=). When calling the variable, we also write the variable name with the $ sign. For example, try to write the following in the file index.php in the theme test:

That means we have created two variables named $ name with the value of the website name and $ url with the value of the website address. Both of these values ​​are taken from get_bloginfo () function in WordPress which will function to get information of our website, about the function concept you will understand more about it in the article introducing functions in PHP.

This also means that in the index.php file, you can use the two declared variables to display the required values ​​instead of rewriting the values. Also, variables are used for other purposes such as testing or comparing. In summary a variable is a data value that can be changed in the program. Data values ​​can be string type (interger), floating number (float), logic (boolean), array (array), object (object) or empty data (NULL).

2. Data types

  • String data (STRING)

The string data type is a text string value, for example setting the name, address, etc. When writing the string in PHP, it must be enclosed in single quotes (‘) or double quotes (“).

Use single quotes when you print out a data string that does not contain special characters and has no need to use special string characters.

You should use quotes sometimes in string data where you need to use special characters or want to declare variable names without concatenating.

  • Natural number data (INTEGER)

Natural numeric data when declared will not be enclosed in double quotes or single quotes.

  • Array Data (ARRAY)

This is a very important and commonly used data type, so here I just give a general overview, and details will have a separate section for it.

Array means a collection of values ​​contained in a variable. Each value will have its own key to represent it.

  • Object data (OBJECT)

This is an advanced data type that I will talk more about in object oriented programming (OOP). An object means a variable created from a class of code.

  • Empty data (NULL)

Empty data means it is declared in memory but has no value.

Thank you for reading the article and wish you success!


Share the news now

Source : Viblo