|
HTML clipboard
All variables in PHP begin with the character dollar "$".
Statement
The variables are initialized simply declare:
$ strCadena = "Hello World";
echo ($ strCadena);
If we tried to access a variable not set, PHP will complain. To avoid this,
there is the function error_reporting(mask)
Types
The basics of PHP types are Integer, Double, String, Array and Object. Boolean
variables do not exist as such but any numeric value other than 0 or any
non-empty string is considered TRUE
PHP variables do not have a fixed rate, depending on the final assignment done,
have one kind or another. The role gettype(nombrevar) to obtain the type of that
variable as a string:
$ variable = "A string";
echo (gettype ($ variable));
$ variable = 0;
echo (gettype ($ variable));
The example above writes "String" and "Integer" on the screen.
The functions Is_Double($varname) Is_Array($varname) Is_String($varname) and
Is_Object($varname) also allow us to know what type of a variable.
Chains
Strings in PHP are surrounded by single quotes or double quotes:
$ strCadena1 = "Hello World <BR>;
echo ($ strCadena1);
$ strCadena2 = 'Hello filthy <BR>';
echo ($ strCadena2);
There is a nuance of difference between the two, we can see with this example:
$ strMessage = "Hello World";
$ strMsgInABottle = "$ strMessage <BR>";
echo ($ strMsgInABottle);
$ strMsgInABottle = '$ strMessage <BR>';
echo ($ strMsgInABottle);
Produces a page with the text
Hello World
$ strMessage
That is, when we use double quotes, expressions of style $varname is replaced by
the value of the variable $varname whereas when we use single quotes, the string
is not evaluated and it is left as is.
Operator to concatenate strings is the point. "
$ strCadena = "Hello";
strCadena $ = $ strCadena. "World";
echo ($ strCadena);
Quotes can include more than one line without any problem:
$ strConsulta = '
SELECT *
FROM
bul_tbl_noticias
WHERE
nombre_autor = \ 'Albert \';
';
As we see, we can escape the quotes with the combination \' By the same token,
\n \t and others have the same meaning as in C.
Arrays
PHP arrays are quite powerful and flexible:
$ arrValores [0] = 1;
$ arrValores [1] = "A string";
echo ( "With \ $ arrValores [0] is $ arrValores [0] and".
"\ $ arrValores [1] is $ arrValores [1] <BR>");
Failure to put the subscript of the element, makes the value assigned to the
next free position in the array. Arrays in PHP start at position 0, so the
previous code could be written more easily as follows:
$ arrValores [] = 1;
$ arrValores [] = "A string";
echo ( "With \ $ arrValores [0] is $ arrValores [0] and".
"\ $ arrValores [1] is $ arrValores [1] <BR>");
A very convenient way of addressing elements of an array is associated. In
the case of associative arrays, rather than accessed by index, accessed by key
or key (the keys themselves are case sensitive, is not the same $arrValores["a"]
to $arrValores["A"] no is the same):
$ arrValores [ "name"] = "Tancredo";
$ arrValores [ "Surname"] = array ( "Gomez", "Jimenez");
echo ( "With \ $ arrValores [\" name \ "] is arrValores $ [name] and".
"at \ $ arrValores [\" Lastname \ "] is."
$ arrValores [ "Surname"] [0]. "And".
$ arrValores [LastName] [1]. "<BR>);
As we see, handle multidimensional arrays in PHP is trivial, just add the
brackets and the subscript desired.
Construction Array() can also be used with associative arrays:
$ arrValores = array (
"name" => "Tancredo"
"Surname" => array ( "Gomez", "Jimenez")
);
Building List() allows us to assign values to an array of variables at once:
$ arrValores = Array (1, "A string", 1.2);
List ($ nNumber, $ strCadena, fnumber $) = $ arrValores;
echo ( "\ $ nNumber worth $ nNumber, \ $ strCadena goes."
" '$ strCadena' and \ $ fnumber worth $ fnumber"); removed as a result:
nNumber worth $ 1, $ strCadena goes' A string 'and $ 1.2
Conversions
To convert a variable from one type to another by using the casting parentheses:
$ strVariable = "5";
$ value = (integer) $ strVariable;
$valor contains the numeric value of the variable $strVariable
We may also use the function SetType($varname, "vartype") to force the variable
$varname is the type vartype
However, PHP is fairly consistent in the types, so if we add a number to a
string, that string becomes a number:
$ strCadena = "5";
echo ( '$ strCadena is of type'. gettype ($ strCadena).
"And worth $ strCadena <BR>");
strCadena $ strCadena + = $ 5;
echo ( '$ strCadena is of type'. gettype ($ strCadena).
"And worth $ strCadena <BR>");
the result
$ strCadena is of type string and is worth 5
$ strCadena is type integer and is worth 10
Should a string concatenated with a number, PHP makes the number to string
conversion automatically:
echo ( "The number is" 5.. "<BR>);
Expected outputs
The number is 5
Variables predeclaradas HTTP
PHP has a whole range of variables predeclaradas to do with HTML, such as:
$PHP_AUTH_USER User authentication.
$PHP_AUTH_TYPE Type of authorization.
$PHP_AUTH_PW Password with which the user is authenticated.
$HTTP_POST_VARS Array with the variables of a form passed by the POST method.
$HTTP_PUT_VARS Array with the variables of a form passed by the PUT method.
Apart from the arrays $HTTP_PUT_VARS and $HTTP_POST_VARS can access the
variables from HTML forms as $ nombrevariable, suppose the following form:
<HTML>
<BODY>
<FORM ACTION="tratar_form.phtml">
<INPUT Type="text" NAME="Nombre">
<INPUT Type="text" NAME="Apellido[0]">
<INPUT Type="text" NAME="Apellido[1]">
<INPUT Type="submit" NAME="btnAceptar" VALUE="Aceptar">
</ FORM>
</ BODY>
</ HTML>
While on page tratar_form.phtml can access the variables of the form to:
Echo ( "Name: $ name <BR>
Apellido1: $ Name [0] <BR>
Apellido2: $ Name [1] <BR>
");
Verification statement
Sometimes it is necessary to know whether a variable has been initialized
already (especially if it comes from an html form, for example), we have the
function IsSet($variable) that lets us know if this variable was already
initialized.
This function must be used in conjunction with an error_reporting(~8) so that
the interpreter does not capture the error of trying to access a variable not
initialized.
|