PHP And Its Variables (basic)

ACT 1 – PHP AND ITS VARIABLES

What we will address here is very simple, they are basic things, but they should be fixed in the mind of who intends to become a PHP programmer. PHP is a programming language created by Rasmus Lerdorf, a native of Greenland. For those who thought that it was just a huge piece of ice where there were only Eskimos and iglus (my case), he was wrong, since one of the most brilliant minds of the last century was born there, later the language was modified by Mr. Andi Gutmans and Zeev Suraski, Phenomenal Israelites the same ones who created ZEND.

Assuming you know how to configure and install PHP, which is now very easy to do with WAMP (for Windows) or LAMP (for Linux), let’s start the practical part.

The following are the php tags.

Everything within the php machine will interpret and return the result to the end user through a browser. The role of the browser is just to show the end user the html corresponding to the php code, the browser will render the code.

The http server (Apache for example) will respond to requests made by the user through the browser, the web server will search for the requested page in the browser’s address bar and the php machine will interpret the codes written on the page and finally the browser will show the result in html, because php is a server-side language, that is, it runs on the server side.

It is on the server that magic happens and not on the client. The php files must be named with a .php extension. It is interesting to follow some conventions, the so-called manners so that your code is easy to understand, documentation is very important. If your file is a class, it is good to be named as follows.

Naming Conventions:

  • file.class.php // for classes
  • file.inc.php // for files you want to include inside others, files with basic settings such as constants and folder addresses essential for the application.
  • file.phtml // a file that contains php and html code on the same page

PHP COMMENTS

Comment is essential for your project documentation, it’s easy to get lost when the project gets big and hundreds of lines of code pop up. To avoid this, it is necessary to comment on the most important parts.

To carry out the comment the following commands are used:

Everything that is typed inside these tags will be discarded by the php interpreter, that is, only the programmer will have access to the comments.

MULTIPLE VARIABLES

VARIABLES

Variables are placeholders in the RAM of the computer because RAM is implied by the fact that the variables are volatile and their values can change so that when the program is closed these values get lost, they are destroyed.

To create a variable in php just choose a name that will be the identifier of the variable preceded by the dollar sign, as follows.

Note that a type has not been defined for the variable, it is not known if it is a number or if it is a word, php is a language that performs typing of variables dynamically, when the programmer defines the value of the variable, automatically php performs variable casting assigning it the required type.

Usually variables are assigned by value, when you assign a value to a variable, it is copied in full to the space in the reserved memory, so that if a user assigns a value from one variable to another, which is changed in a variable it will not be reflected in the other, because they are distinct memory spaces.

The variables in PHP do not lack an initial value given by the user, however, it is a good practice to do so. To test whether a variable has been initialized, simply use the isset () function, by default they are started by the system with the following values.

  • For Boolean type always start in FALSE.
  • When integers and floating points start at 0.
  • In case strings are empty.
  • Arrays are also empty.

There are some rules that should be considered when naming a variable, they are:

  • PHP is case sensitive, so $ variable is different from $ vAriAblE
  • A valid variable name starts with a letter or an underline sign followed by any number of numbers, letters, or figures.

TYPES OF VARIABLES

Although PHP is not typed, we can define here the main types of data that are included in php dynamically in the total of 8, being 4 scalar types, 2 compound types, and 2 special types.

Boolean type: Assumes only two values, either is TRUE or FALSE, a boolean expression can be set to false or true, a conditional structure such as if for example returns a boolean according to the result of the condition. The Boolean type was entered in php4, both the TRUE and FALSE reserved words are not case-sensitive.

String type: is a string, the characters are formed by a byte. A string can be included in several ways among them we can mention:

Single quotes: Anything between single quotes will be considered String, including identifiers preceded by dollar signatures.

Double quotes: interprets both strings and variables and escape characters.

Ex.:

Syntax HEREDOC is quite unusual, it has the advantage of leaving text exactly as typed, interpreting even line breaks, as well as behaving like double-quoted text, however, it is not necessary to escape the characters of double quotes and single quotation marks.

Ex.:

Integer type: These are integer numeric values. Which can be used in decimal, octal or hexadecimal base.

Type float or double: These are real numeric values that allow the use of floating points. These can be represented containing decimal places.

Array: It is a type that stores several indexes in memory, the indexes function as keys that store the memory location where the variables are, the array can be a vector (linear), it can be a list, a stack, a queue, to create an array in php is simple and can be done in several ways.

Object: it is a very powerful resource that would yield many pages of study, however here we will just make a synthesis about it, an object can be created from a class and usually has attributes and methods, can be created as follows.

Resource: is a special type of variable that holds a reference in memory that will be used by an external resource, usually used by a file manipulator, a connection to the database, among others.

NULL: a variable can be considered null in 3 ways:

  • If it is assigned NULL.
  • Because of the use of the unset ($ var) command that erases the variable.
  • Or if she has not received any value.

PSEUDO-TYPES

Mixed: means that a parameter can accept several different types of variables, such as the gettype () function, where the user can enter an argument of any type, mixed means mixed.

Number: It means that a parameter can be either integer or float.

CallBack: It is a parameter that requests the execution of an external function, can be the name of a function or method of an object. In other words, it is a function that has an argument another function being that the latter will be defined by the user. Call_user_func () is an example of a function that accepts callback parameters.

Void: void means empty, when it is a return type it indicates that there is no value to be returned, and inside the list of arguments it indicates that the function does not accept arguments.

SUPERGLOBALS, GLOBALS, LOCATIONS

The variables are limited to a certain scope, some can be accessed from anywhere in the application, these are global variables, the locations can only be accessed within a certain scope such as the variables of functions so that a local variable can be converted into global just do:

Superglobals are variables pre-defined by php, such as the environment variables $ _SERVER, HTTP_VARS  $ _POST, $ _GET, $ _FILES, and so on.

STATIC VARIABLES

The static modifier is used to keep the value of the variables inside a function, whenever a function is executed, at the end of its execution the value of the variables is zeroed, when using static these values remain inert.

Ex.:

It would be very useful in a hit counter to use static variables. When using recursion, the static modifier is also useful.

VARIANT VARIABLES

Occurs when two or more dollar signs are added to a particular variable. The php ends up referring to a variable through the content of the first one.

Ex.:

EXTERNAL VARIABLES

They are variables sent through forms, the parameter is usually informed by the end user, through the superglobals $ _POST and $ _GET, for example, it is possible to capture these external variables. Follow the example:

HTML Form:

PHP code to retrieve the value entered in the txtName text box. Suppose you entered the “username” and “userpassword” values in the text box.

$ _POST is a superglobal array variable:

Another way to call form variables is by using the import_request_variables (“String type”, “String prefix”) function. That transforms the $ _POST into a global.

$ _GET is an array that takes the data directly from the page URL, it will store the content that comes after the question mark “?”.

www.site.com/index.php?area=home

echo $ _GET [“home”]; // home

COOKIES

They are variables used to record information on the client computer, which is widely used in shopping sites. This makes information processing faster since there is already a preview of the site content on the client computer and some important information such as username and password. The setcookie () function performs the procedure for writing cookies, this must be the first thing to be executed by the browser, it is in the header of the browser.

REFERENCES

https://www.php.net/

Leave a Reply