Tag: Basics

Basics of Php Variables

Basics of PHP – Part 3

Introduction
This is part 3 of my series, Basics of PHP. In this part of the series, I give you the basics of PHP variables. PHP has variables, similar to mathematical variables. In the strict sense, they do not behave like mathematical variables. In this part of the series, we look at the meaning of PHP variables and some of the type of values that can be assigned to them. The variable name is the name, you, the programmer give.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Example
Consider the following statement;

    $ myStr = “This is the third part”;

In the above statement, the variable is, $ myStr, meaning, my String. The value “This is the third part” is in quotes. It is a string. It is assigned to the variable, $ myStr, using what is called the assignment operator, “=”.

A variable begins with a dollar sign followed by the name of the variable. A valid variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores.

Consider now the following two consecutive statements:

    $ myStr = “This is the third part”;
    echo $ myStr;

The first statement assigns the value, “This is the third part” to the variable, $ myStr. The second statement sends the value assigned to the variable to the browser. The second statement has the, echo construct. What goes next to the construct, echo, is called an argument to echo. So $ myStr is an argument to echo. There are many situations in computing where you would use the variable instead of the value. Try the following code:

    <?php

        $ myStr = “This is the third part”;
        echo $ myStr;

    ?>
Numbers as Values
You can use numbers as values. You can assign a number to a variable. Consider the following two statements:

    $ myNum = 56ረ
    echo $ myNum;

The number, 56.48 is assigned to the variable, myNum. When you are assigning a number, you do not have to put the number in quotes. The second line sends the number to the browser. Here, the argument to the echo construct is, myNum. Try the following code:

    <?php

    $ myNum = 56.48;
    echo $ myNum;

    ?>

Assigning a Variable to another Variable
You can assign a variable to another variable. Consider the following two statements:

    $ str1 = “test”;
    $ str2 = str1;

The first statement assigns the string value, “test” to the variable, $ str1. The second statement assigns the variable, $ str1 to $ str2. The value of $ str2 is “test” copied from str1.

Changing the Value for a Variable
You can assign a value to a variable and then change it after that. Consider the following statements:

    $ myStr = “test”;
    $ myStr = “good”;

The first statement assigns the value, “test” to the variable, $ myStr. The second statement assigns a new value to the same variable. The final value of, $ myStr is “good”.

Constant
In the above section, the value, “test” is first assigned to $ myStr and then “good” is assigned to the same variable. This means the variable $ myStr is not constant. If you want to prevent a variable from being assigned a new value, you should precede the statement with a reserved word, which is, const. A reserved word is a word you do not use arbitrarily in code. The following code illustrates this. Try it and notice that the value of the variable, $ myStr is not changed (the code may not work with your version of PHP).

    <?php

    const $ myStr = “beautiful”;
             $ myStr = “handsome”;
    echo $ myStr;

    ?>

Boolean Variable
In some situations, you can have one of two possible values. The value can either be, true or false. Any variable that deals with these two values is called a Boolean variable. So, you can have something like:

    $ myVar = true;

           or

    $ myVar = false;

You do not need quotation makes around true or false.

Rule for Naming a Variable
A variable name must start with a letter or underscore, ‘_’. Within the name and at its end, you can have a letter, number or underscore. You precede all that with a $ sign.

Case Sensitivity in PHP
PHP is said to be case sensitive. This means that for variable names, $ myVar is not the same as $ MyVar or $ myvar or $ MYVAR, etc.

Creating a Variable
Before you can use a variable, you have to create it. To create a variable, you begin with the $ sign and then the name of the variable; just as we have been doing above. You do not have to assign a value to a variable when you create it (but end it with a semicolon). You can do the assignment later. The following code illustrates this:

    <?php
   
        $ myVar;
        $ myVar = “you”;
        echo $ myVar;
   
    ?>

When to use Quotation Marks for Values
If your value is a number or a Boolean value, you do not have to use quotation marks. If your value is a string, you must use quotations marks.

String
A string is a value in quotes. The quotes may be single (‘) or double (“).

Integer
An integer is a whole number. It could be a negative number, zero or a positive number.

Floating Point Number
A floating-point number is a number with a decimal point. There are several ways of writing it.

Boolean Value
A Boolean value is either true or false. Now, 0 is equivalent to false. Any other number is equivalent to true. Note: an empty string (“”) is equivalent to false.

Literals
When used as a value, a string, integer, floating-point number or Boolean value is called a literal.

We have seen a lot. Let us take a break here and continue in the next part of the series.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):

Getting started with PHP
PHP Basic Syntax
Basics of PHP Variables
PHP Conditional Statements
Boolean Logic for PHP
Boolean Logic and PHP Conditions
PHP Comparison and Arithmetic Operators
PHP Loop Statements
PHP Function Basics
PHP Array
Some PHP Predefined Functions and Arrays
PHP Variable Scope Basics
PHP Object Basics
PHP Reference
PHP Error Basics
PHP String and Date Basics
PHP Form Simple Validation with embedded Error Messages from Server
 

Written by Chrys

Ultimate Hd Video Background Box
Over 600 high quality, royalty free Video Backgrounds and Audio Loops, Sound Effects. Ideal as an Add-On Sales to any kind for Multimedia and Video Production Product, Video Presentation, Video Training, Video Learning, PowerPoint Presentation…
Ultimate Hd Video Background Box

The while loop, it’s syntax, and an example of counting numbers. WEBSITE phpacademy.org FORUM http TWITTER twitter.com FACEBOOK www.facebook.com


Xhtml Table Basics

XHTML Basics – Part 13

Note: If you cannot see the code or if you think anything is missing (broken link), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what has been typed.

Introduction
Any information that you have in a grid form should be put in a table. An example of such information is the marks of students during an examination. This is an illustration of the arrangement of such marks:

            History  Math  Geography
John        75         60          70       
Peter       50         55           80
Mary       54         62           73

In this example there are four rows and four columns. The first row has texts. The first column also has texts. A table can be made up of any number of rows and columns. Each item (word or number) above is said to be in a cell. So a table is made up of cells. Each cell can take numbers or text or both numbers and text.

In this tutorial series, you should try all the code samples to really appreciate what is going on.

Cells
A table is made up of cells. The following example shows how the cells can be identified:

Cell00 Cell01 Cell02 Cell03
Cell10 Cell11 Cell12 Cell13
Cell20 Cell21 Cell22 Cell23
Cell30 Cellǿ Cell32 Cell33

This cell arrangement would receive the table of marks above. The number of rows here are the same number of rows above; and the number of columns here are the same number of columns above. In computing, counting begins from zero (not one). There are four rows in this arrangement. The rows are identified as row 0, row 1, row 2, and row 3. The columns are identified as column 0, column 1, column 2 and column 3. You do not have row 4 and column 4 here, since counting in computing begins from zero.

Each cell identifier above begins with the word “Cell” followed by two digits. The first digit is the row number and the second digit is the column number. Fitting the above table of marks into the cell arrangement, nothing would go into Cell00, “History” would go into Cell01; “Math” would go into Cell02, “John” would go into Cell10, “75″ would go into Cell11, and “60″ would go into Cell12, and so on, until “73″ would go into Cell33.

The Code
In this section, we see the tags used to define an XHTML table. The table element contains other elements. The table of marks in the introduction above, would fit into the following table element (see explanation below):

    <table>
        <tbody>
            <tr>
                <td> </td><td>History</td><td>Math</td><td>Geography</td>
            </tr>
            <tr>
                <td>John</td><td>75</td><td>60</td><td>70</td>
            </tr>
            <tr>
                <td>Peter</td><td>50</td><td>55</td><td>80</td>
            </tr>
            <tr>
                <td>Mary</td><td>54</td><td>62</td><td>73</td>
            </tr>
        </tbody>
    </table>

The TABLE element above has what is called the TBODY (table body) element; it has TR (table row) elements, and TD (table data) elements. The TD element contains the item you see at the browser. The table tag name is “table” in lower case; the table body tag name is “tbody” in lower case; the row tag name is “tr” in lower case and the table data tag name is “td” in lower case. Each of these elements has a start and end tag.

There is only one table, so there is only one table element. There are four rows in the table, so there are four TR elements. Note that there are no column elements. Columns are defined as you place TD elements as content for the TR element. There are four columns, so each TR element has four TD elements. Each item in the table of marks at the introduction above is content to a TD element.

You must have noticed the content, “ ” in the TD element of cell00. “ ” is the character entity for a space (pressing spacebar key once).  In theory, no TD element should be left without content. Note that we did not have to put any item in cell00. In theory, if you leave a TD element without content, the TD element will collapse; this means that at the browser, you will not see the area occupied by the TD element. So in theory, any TD element without content must have, “ ”. In this way TD elements without content will have the area of one character space at the browser. In practice, if in a row or column, you have “ ” in one of the TD elements and the rest of the TD elements in the row or column do not have any content, then all the cells in the row or column will display a one-space character area at the browser.

Now, try the above code with the following XHTML document:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
</head>
<body>
    <table>
        <tbody>
            <tr>
                <td> </td><td>History</td><td>Math</td><td>Geography</td>
            </tr>
            <tr>
                <td>John</td><td>75</td><td>60</td><td>70</td>
            </tr>
            <tr>
                <td>Peter</td><td>50</td><td>55</td><td>80</td>
            </tr>
            <tr>
                <td>Mary</td><td>54</td><td>62</td><td>73</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

The TH element
In the above table, you may want to have the items for the first row (History, Math, etc.) and the items for the first column (John, Peter, etc.) automatically bolded. To achieve this, replaces the TD elements for the first row and column with the TH elements. The syntax for the TH element is

                 <th>Content</th>

Here, tag name is in lower case; start and end tags are required. Try the following code that has the replacement:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
</head>
<body>
    <table>
        <tbody>
            <tr>
                <th> </th><th>History</th><th>Math</th><th>Geography</th>
            </tr>
            <tr>
                <th>John</th><td>75</td><td>60</td><td>70</td>
            </tr>
            <tr>
                <th>Peter</th><td>50</td><td>55</td><td>80</td>
            </tr>
            <tr>
                <th>Mary</th><td>54</td><td>62</td><td>73</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

Wow, table designing is not as difficult as some thought. It is actually easy. Read on.

Border
You can separate all the cells at the browser with border. To achieve this, just add the attribute, border in the start tag of the table element as follows:

    <table border>

The border attribute can optionally take a value. Here it does not take a value. Modify the start TABLE tag in the above code with the border attribute and redisplay the XHTML document (in your browser); avoid making a mistake, because if you make a mistake, you either will not see any table at all, or you will see scattered text.

Cell Spacing and Cell Padding
Cell spacing is the space between cells. Cell padding is the space between cell content and its borders. You can control the amount of cell spacing and cell padding you want. To do this, use the cellspacing and cellpadding attributes correspondingly. You do not have to use both attributes in a table. If you do not use any of these attributes, the browser chooses a value for you, as in the above XHTML document. If you want a cell spacing of 8 pixels you would add the following to the table start tag:

              cellspacing=”8″

A pixel is the size of the


  • Copyright © 1996-2010 Programming tutorials for beginners,. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress