Guide to Learning PHP

A Beginner’s Guide to Learning PHP

PHP is a server-side programming language used on the Internet to create dynamic web pages. It is often combined with MySQL, a relational database server that can store the information and variables that PHP files can use. Together they can create everything from the simplest website to a complete business website, an interactive web forum, or even an online role-playing game.

Before we can do the big and sophisticated things, we must first learn the basics from which we build.

  1. Start by creating a blank file with any program that can save in plain text format.
  2. Save your file as an a. PHP file, for example, mypage.php. Saving a page with the .php extension tells your server that it will need to execute the PHP code.
  3. Enter the statement so the server knows that PHP code is on the way.
  4. After this, we would enter the body of our PHP program.
  5. Enter the statement?> So that the browser knows that the PHP code is ready.

Each section of the PHP code begins and ends by activating and deactivating the PHP tags so that the server knows that it needs to execute PHP between them. Here is an example:

//on

//and

//off ?>

Everything in between is read as PHP code. The statement can also be drafted in the simplest way if desired. Anything outside of these PHP tags is read as HTML, so you can easily switch between PHP and HTML as needed. This will come in handy later in our lessons.

Comments

If you want something to be ignored (a comment, for example), you can put // in front like I did in our example on the previous page. There are a few other ways to create comments within PHP, which I will demonstrate below:

//A comment on a single line

#Another single line comment

/* Using this method you can create a larger block of text and it will all be commented out */

?>

One reason you may want to comment on your code is to make a note of what the code is doing for reference when you edit it later. You can also put comments in your code if you plan to share it with others and want them to understand what you do, or include your name and terms of use in the script.

PRINT and ECHO statements

Let’s first learn about the echo statement, the most basic declaration in PHP. What it does is generate whatever you tell it to echo. For example:

This would return the Like About statement. Notice that when we echo a statement, it is enclosed in quotation marks [“” ??].

Another way to do this is to use the print function. An example of that would be:

There is a lot of debate as to which one is best to use or if there is a difference. Apparently, in very large programs that simply output text, the ECHO instruction will run a bit faster, but for beginners they are interchangeable.

Another thing to keep in mind is that all of your print / echo is contained in quotes. If you want to use quotes within your code, you must use a backslash:

\”I like About too\”” ?>

PHP Test Page

“;

print “Billy said \”I like About too\””

?>

As you can see, you can insert HTML directly into your php print line. You can format the HTML in the rest of the document however you like, but remember to save it as a .php file.

Do you use PRINT or ECHO? Share your answer!

Variables

The next basic thing you need to learn how to do is set a variable. A variable is something that represents another value.

This sets our variable, $ like, to our previous Like About statement. Notice again the quotation marks [“” ??] used, as well as the semicolon [;] to show the end of the statement. The second variable $ num is an integer and therefore does not use quotation marks. The next line prints the variable $ like and $ num respectively. You can print more than one variable on a line using a period [.], For example:

“;

print $like . ” ” . $num;

print ”

“; print “My favorite number is $num”; ?>

This shows two print examples of more than one thing. The first print line prints the variables $ like and $ num, with the dot [.] To separate them. The third print line prints the $ as a variable, a blank space, and the variable $ num, all separated by periods. The fifth line also demonstrates how you can use a variable enclosed in quotes [“”].

A few things to remember when working with variables: they are CaSe SeNsitiVe, they are always defined with a $ and must start with a letter or underscore (not a number). Also, note that if necessary, it is possible to dynamically construct variables.

Arrays

While a variable can contain a single piece of data, an array can contain a string of related data. Its use may not be immediately apparent, but it will become clearer as we begin to use loops and MySQL. Here is an example:

$age[“Justin”] = 45; $age[“Lloyd”] = 32; $age[“Alexa”] = 26; $age[“Devron”] = 15;

print “My friends names are ” . $friend[0] . “, ” . $friend[1] . “, ” . $friend[2] . “, and ” . $friend[3];

print ”

“;

print “Alexa is ” . $age[“Alexa”] . ” years old”; ?>

The first array ($ friend) is organized using integers as the key (the key is the information in [brackets]) which is useful when using loops. The second array ($ age) shows that you can also use a string (text) as a key. As demonstrated, values ​​are called by print in the same way that a regular variable would.

The same principles apply to arrays as variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or underscore (not a number).

Operands

Everyone has probably heard the term expression used in math. We use expressions in PHP to perform operations and respond to a single value. These expressions are made up of two parts, operators and operands. The operands can be variables, numbers, strings, Boolean values, or other expressions. Here is an example:

a = 3 + 4

In this expression, the operands are a, 3 and 4

b = (3 + 4) / 2

In this expression, the expression (3 + 4) is used as an operand along with by 2.

Operators

Now that you understand what an operating, we can go into more detail about are the operators. Operators tell us what to do with operands and fall into three main categories:

Math:

+ (plus), – (minus), / (divided by) and * (multiplied by)

Comparison:

> (greater than), <(less than), == (equal to) and! = (Not equal to)

Boolean:

&& (true if both operands are true), || (true if at least one operand is true), xor (true if ONLY one operand is true) and! (true if a single operand is false)

Mathematical operators are exactly what they are called, they apply mathematical functions to the operands. The comparison is also quite simple, they compare one operand with another operand. Boolean, however, may need a more detailed explanation.

Boolean is an extremely simple form of logic. In Boolean, each statement is either true or false. Think of a light switch, it must be on or off, there is nothing in between. Let me give you an example:

$ a = true;

$ b = true;

$ c = false;

$ a && $ b;

This is asking $ a and $ b to be both true, since both are true, this expression is TRUE

$ a || $ b;

This is asking for either $ a or $ b to be true. Again this is a TRUE expression

$ a xor $ b;

This is asking either $ a or $ b, but not both, to be true. Since both are true, this expression is FALSE

! $ a;

This is asking $ a to be false. Since $ a is true, this expression is FALSE

! $ c;

This is asking $ c to be false. Since that’s the case, this expression is TRUE

Conditional statements

Conditionals allow your program to make decisions. Following the same kind of Boolean logic you just learned, the computer can only make two choices; true or false. In the case of PHP, this is accomplished using IF-ELSE statements. Below is an example of an IF statement that would apply a senior discount. If $ over65 is false, everything in {brackets} is simply ignored.

However, sometimes IF statement alone is not enough, you also need an ELSE statement. When only the IF statement is used, the code in brackets will be executed (true) or not (false) before continuing with the rest of the program. When we add the ELSE statement, if the statement is true, it will execute the first set of code and if it is false, it will execute the second set of code (ELSE). Here is an example:

Nested conditionals

One useful thing to remember about conditional statements is that they can be nested within each other. Below is an example of how the discount program in our example could be written to use nested IF ELSE statements. There are other ways to do this, like using else if () or switch (), but this demonstrates how statements can be nested.

65)

{

$discount =.90;

print “You have received our senior’s discount, your price is $” . $price*$discount;

}

else

{

if ($age

This program will first check to see if they are eligible for the senior discount. If they are not, it will check to see if they are eligible for a student discount before returning the non-discounted price.

Keep reading PHP Webquest at the Internet Congress in the Classroom

No Comments

Post A Comment