We will dedicate this post to variables in PHP and finish creating our first PHP. First, what is a variable? A variable could be described as a symbolic name that takes a specific value. To clarify this definition:
• An integer (0, 1, 2) is that number in itself and for all (the 1 is not ever going to be 2 .)
• A variable that we call, for example, mi_numero, contains a value (to continue with the example, an integer value) and can be changed by assigning a different value. Thus the sentence:
• mi_numero = 1 causes the variable mi_numero, when asked, say that takes the value 1.
• mi_numero = 1 + 5 makes the variable takes the value 6.
• Etc etc
But a variable can be of different types need not always be a number. So, what types of variables we can find in PHP? Generalizing too much, the most common types are:
• Simple Types: Integer, Float, Character, Boolean.
compounds
• Types: Arrays, Strings.
Simple Types:
• Full: numeric value (positive or negative) without decimals (-10, 0, 1, 100,000, ...).
• Float: value (positive or negative) fractions (15.45, -20.1895, ...).
• Character: alphanumeric value determined by converting a code table (usually ASCII). To understand and generalize a lot, a letter or symbol on your keyboard ('a', 'b', 'C', '-', '+', ...). Note that I've used a simple quotation marks (') because that's how the program will then be used in PHP.
• Boolean: a value yes or no, as if it were a switch. In PHP you use the values \u200b\u200btrue or false (true or false).
Composite Types:
• Array (vector, matriz): sobre arrays se podría dedicar un tema entero a hablar de ellos. Un array es una colección de objetos referenciada bajo un único nombre. Todos los objetos que contiene son del mismo tipo. Podemos hablar de un array de enteros, un array de flotantes, de caracteres, etc. Por ejemplo, supongamos que queremos almacenar la combinación de la lotería primitiva. En dicho caso, podríamos utilizar una variable llamada “primitiva”, de tipo array, que contuviera los valores de este modo:
• primitiva[0] = 10;
• primitiva[1] = 12;
• primitive [2] = 20;
• primitive [3] = 29;
• primitive [4] = 41 ;
• primitive [5] = 47;
Notes
displayed the symbols "[" and "]". These serve to indicate, within the array, what position we want to access. Thus, primitive [0] is a reference to the first element of the array, primitive [1] the second element, etc.. What we have done with the above code is assigned to the first 6 elements of the array of 6 values \u200b\u200bof the winning combination (10, 12, 20, 29, 41 and 47).
is possible in PHP, to assign each array element value of different types. That is, in the first position you have an integer, the second a character, etc etc. This, which a priori appears very simple, do not allow most programming languages \u200b\u200bunless you create your own data structure. In PHP is not necessary to have data structures as a separate type to have this functionality. Consider, for example, that you have a variable "demand" that symbolizes the request a client, and you assign these values:
• request [0] = "15/01/2008";
• Order [1] = "suitcase"
• Order [2] = 2;
If you program when you think, "in the first place I'll always save the date, the second the product name and the third the quantity ordered ... "this array makes sense, right? And notice that while the first two positions are of type string (now we shall see in detail what are) the latter, the quantity is numeric.
• strings: A combination of alphanumeric characters. For example, "Hi, this is a string" would be a string (note that I used the double quotation marks (") to delimit). Also could devote another whole chapter to discuss the handling of strings. One detail to indicate that a string is a subtype of type array plus a number of properties added. Thus, the following piece of code:
• my_string = "Hi I am a string";
• my_string [0] = 'P';
is perfectly Vá ; valid. In the first statement I have assigned to a variable called "my_string" value "Hello I'm a String." In the next sentence, I modified the first character string giving the value "P". If now consultásemos the value of "my_string" the result would be "I'm a Chain Pola." That is, the items a string can be referenced as an array of characters were talking. Syntax
variables in PHP
Well, we know (or have we made an idea) of the basic types that exist in PHP variables. How to use a variable in PHP?
Variables in PHP are preceded by the symbol $. So
• $ number = 5; makes you think the variable number and assigned value 5.
• $ number [0] = 'K', does that create an array called numbers and his first position is assigned the value 'K';
Traditionally, programming languages \u200b\u200bwe "demand" that explicitly declare the variable type (ie, whether to contain an integer , if you will contain a boolean ...) or even, when we talk about complex data types, pre-allocate memory that can hold in your computer. With PHP you do not need none of the above, the mere fact of referencing a variable with the $ sign preceding it and to "provoke" both the creation of the variable as memory allocation. PHP is, as we usually call technologies information, a weakly typed language, it does not require explicitly knowing the type of variables used. The benefits are enormous, not only by not having to manage memory space but being able to create and assign variables at the time they are needed without having to remember or type of nomenclature. In addition, PHP lets you do things like
• $ number = 5;
• $ number = "hello";
Where, in the first sentence we created a variable number and have given value 5 and immediately afterwards, to the same variable we assigned the value "hello", which we know is a character string that requires storage than a number and its philosophy of use and permitted operations are different from the integer .
The main against this use of variables is that a mistake in the name of a variable is not directly detected. For example, in a strongly typed language like C, if we do:
• int number = 5;
• numro = 6;
language gives us an error because the variable numro not exist (there number, but we ate rewrite the "e").
with PHP, though:
• $ number = 5;
• $ numro = 6;
causes the creation of two variables, number and numro. If later we refer to the variable number thinking that its value is 6, we will have made a mistake and will detect only check the code.
Our first PHP
Well, it's time to get down to work. Like all programming books, we begin with the program simpler (in this case, the easiest web). The classic "Hello World." His goal is nothing to show, on screen, the text "Hello World."
Since we are dealing with websites, the content should be included within the format of a web page. In other words, we must generate a HTML PHP when you want to display something on the screen (ie maximum applicable to any web page that uses server-side language).
The simplest structure that contains a Web page is:
\u0026lt;HTML>
\u0026lt;HEAD>
\u0026lt;/ HEAD>
\u0026lt;BODY>
\u0026lt;/ BODY>
\u0026lt;/ HTML>
Breaking down a bit:
• ; All HTML mark is enclosed in the symbols "\u0026lt;" and ">".
• The blocks are HTML tags that open and close. For example, \u0026lt;HTML> indicates that we are entering a HTML page and \u0026lt;/ HTML> indicates the end of it.
• A block may contain more blocks. For example, the block containing blocks \u0026lt;HTML> \u0026lt;HEAD> and \u0026lt;BODY>
• HEAD section of every HTML page contains information about BP Page not visible in the browser (or at least to view we are required to explicitly inquire within your browser, information about the page) unless TITLE section, which is used for give a title to the page (the title that appears in the top bar of your browser window.)
• The BODY is the body section of the page, those data structures, sections, etc. that you want to display.
Only this, and we have a page in HTML format (of course, blank, shows nothing, but the browser "understands"). If we want to include PHP code, we should do between the "\u0026lt;?" To start and "?>" To finish it (without the quotes.
Our "Hello World" in PHP would be something like the following:
\u0026lt;html>
\u0026lt;head>
\u0026lt;title> Cap 3, Example 1 \u0026lt;/ title>
\u0026lt;/ head>
\u0026lt;body>
\u0026lt;? echo "Hello World";?>
\u0026lt;/ body>
\u0026lt;/ html>
not hard to see that we have added Title Section Head in to give a title to the page (if not included, will show "Untitled Document "on the top bar) and we've added a section with PHP code:
\u0026lt;? echo "Hello World";?>
What have we done here? We used a function PHP echo , used to show something. Note that all PHP statements end with the character "." PHP echo function always "hoped" that prompted a parameter to display. How do I sign? Including the outcome of the parameter in the HTML then return. The above PHP generate, for example, the HTML for the browser:
\u0026lt;html>
\u0026lt;head>
\u0026lt;title> Cap 3, example 1 \u0026lt;/ title>
\u0026lt;/ head>
\u0026lt;body>
Hello World
\u0026lt;/ body>
\u0026lt;/ html> ;
The result of the string "Hello World" is just such a text, so we included as a result of the PHP section we had created.
Anyone can say "well, but I do not need make a PHP. I make the resulting page and I'm done. " And it's true. For the above example is not necessary because PHP HTML static content and we could have created. But now, consider the following PHP:
\u0026lt;html>
\u0026lt;head>
\u0026lt;title> Cap 3, example 2 \u0026lt;/ title>
\u0026lt;/ head> ;
\u0026lt;body>
\u0026lt;?
$ phrase = rand (0.2);
if ($ sentence == 0) echo "Hello World";
if ($ words == 1) echo "Hello Continent "
if ($ sentence == 2) echo" Hello City ";
?>
\u0026lt;/ body>
\u0026lt;/ html>
Here are elements that still We have not commented. Although we will discuss, is sufficient to state, to understand the example, that:
• Rand (minimum, maximum) is a function that returns a random number between the minimum and maximum prompted.
• If a sentence. Evaluate the condition that prompted. If true, executes the statements that he directed.
• $ phrase == When we're doing a comparison number (put == in PHP to say "if the left is equal to the right").
What does this PHP?
• Creates the variable $ sentence and assigned a random value between 0 and 2.
• If the variable $ sentence is 0, displays the string "Hello World."
• If the variable $ sentence is 1, displays the string "Hello continent."
• If the variable $ sentence is 2, displays the string "Hello City".
The HTML output can vary WHENEVER CALL TO PAGE so we know to be one of the three combinations, but each time you access this PHP the result will be different.
You can try the two examples in these directions:
First example: http://phpdesdecero.host.sk/c3-e1.php
Second example: http://phpdesdecero.host. sk/c3-e2.php
0 comments:
Post a Comment