Pages

Saturday, August 27, 2011

Variables Of PHP






Variables are used for storing values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;
For example :
<?php
$txt="Hello World!";
$x=20;
?>


in PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.

Naming Rules for Variables :

  • A variable name must start with a letter or an underscore "_"
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)

The if Statement Of PHP


The if Statement Of PHP

Use the if statement to execute some code only if a specified condition is true.

Syntax :

if (condition) code to be executed if condition is true;
The following example will output "Have a nice weekend!" if the current day is Friday:
<html>
<body>

<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice day!";
?>

</body>
</html>



PHP If...Else


Use the if....else statement to execute some code if a condition is true and another code if a condition is false.

Syntax

if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

For Example :

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>

</body>
</html>


Let Try.....

Boolean



Boolean is simple syntax of PHP, for execution True or False which case sensitive.
Syntax : $eat =TRUE;

For Example :

<html>
<head>
<title>Boolean</title>
</head>

<body>
<h1> boolean example</h1>
<pre>
$a = TRUE;
$b = FALSE;
</pre>
Execution with PHP: <br>
<?php
$a = TRUE;
$b = FALSE;
echo “\$a = $a”.”<br>”;
echo “\$b = $b”;
?>
</body>
</html>


LET'S TRY

Array






An array is a special variable, which can store multiple values in one single variable.
If you have a list of items, for example car, storing the cars in single variables could look like this:
$cars1="Honda";
$cars2="Toyota";
$cars3="Marcedes";



An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Each element in the array has its own index so that it can be easily accessed.
there are three kind of PHP arrays:
  • Numeric array
  • Associative array
  • Multidimensional array

Numeric Arrays

A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Honda","Toyota","Marcedes");
2. In the following example i assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";




Associative Arrays

An associative array, each ID key is associated with a value.
For example i use an array to assign old to the different cars:
$old = array("Honda"=>32, "Toyota"=>30, "Marcedes"=>34);






Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.









Learning PHP Now!!!



About to build a dinamic web pages, you must to learn PHP. I can help you to learn PHP from basic to expert with my article about PHP. We start from Basic PHP syntax.

Basic Syntax
A PHP scripting block always starts with <?php or <? and ends with ?> . A PHP scripting block can be placed anywhere in the document.
For maximum compatibility, i recommend that you use the standard form (<?php) rather than the shorthand form.
<?php
?>
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Below, I have an example of a simple PHP script which sends the text "Hello World" to the browser:
<html>
<head>
<title>Hello</title>
</head>
<body>

<?php
echo "Hello World";
?>

</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World".





Embedded Script


Embedded Script
Embedded script is A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
For example :
<html>
<head>
<title>Embedded Script</title>
</head>

<body>
<?php
echo “ Let learn about PHP 5”;
?>
</body>
</html>

Sunday, August 7, 2011

The Use PHP & MySQL

Both PHP and MySQL are popular web hosting applications. It is safe to say that both PHP and MySQL are essential components when it comes to running the popular Content Management Systems. A prime example of the popularity and the essentialness of PHP and MySQL can be seen within Wikipedia. This is because wiki runs on what is known as Mediawiki software; this software is wrote using PHP script and uses a MySQL database.
So what exactly is PHP and MySQL?
PHP (a recursive acronym for PHP: Hypertext Pre-processor) is a form of computer scripting language, which was originally designed for the production of dynamic web pages. It is a widely used general purpose scripting language, which is especially suited for web development as it can easily be embedded into HTML and generally runs on a web server, taking PHP code as its input and creating web pages as output. It can be deployed on most web servers and on almost every operating system and platform free of charge. So far it is safe to say that PHP is installed on more than 20 million websites as well as one million web servers. PHP is also the most popular Apache module among computers using Apache as a web server. Apache is primarily used to serve both static content and dynamic Web pages on the World Wide Web. Many web applications are designed expecting the environment and features that Apache provides.
Recently, on May 1st 2008 the most major release of PHP came out, which is version 5.2.6. This version, as with previous versions can be developed on most web servers as well as many operating systems and platforms; it can also be used with many relational database management systems. Also the PHP Group provides the complete source code, which allows users to build and customise as well as extend it to fit their own use.
PHP's programming syntax is very similar to that of a programme called Perl and due to the elegant design used, PHP makes it possible for anyone to learn how to use it. Also, unlike other scripting languages, PHP supports the most common databases like Oracle, Sybase and MySQL. It is also possible to integrate external libraries to generate PDF documents or parsing XML. PHP is an open-source language and is supported by a large group of developers.
MySQL
MySQL is a database management system. MySQL is what is known as an Open Source SQL database, SQL is a standard interactive and programming language for querying and modifying data and managing databases. MySQL is very fast, reliable, and easy to use. It also has a very practical set of features developed in very close cooperation with its users. When MySQL was originally developed it was done so as a way of handling very large databases, which are much faster than existing solutions and have been successfully used in highly demanding production environments for many years. MySQL is also one programme that is under constant development and today MySQL offers a rich and very useful set of functions. The connectivity, speed and security that are brought to you by MySQL makes it highly suited for accessing databases on the Internet. It has more than 11 million installations and runs as a server providing multi-user access to a number of databases.
Helen is the web master of Discount Domains, specialists in the use of PHP and MySQL
Please feel free to republish this article provided a working hyperlink remains to our site
Article Source: http://EzineArticles.com/?expert=Helen_Cox

Beginning PHP and MySQL

PHP or PHP Hypertext Preprocessor is a very powerful open source server side scripting language used for creating dynamic web pages. A Server side scripting language is a language that is read on the server side and not the client side. When a user accesses a web site written in PHP the web server will read the page and output HTML to the users web browser. HTML is different than PHP in that HTML is a client side language, when a user requests an HTML page the web server simply send the text in the HTML page to the users browser which then formats that page based on the HTML. MySQL is also a free open source software the is used to create databases that are used primarily by web servers. The My stands for the name of the co-founders daughter, My while the SQL stands for structured Query Language.
Mixing PHP and MySQL together in a website will give you unlimited possibilities of what you can produce. One little problem with developing websites in PHP is that since it is a server side scripting language you have to open the page from a web server to view it properly. Unlike HTML where you can save an HTML document on your desktop and then view it from there, PHP needs that server interaction to view properly. The good news is most web hosting companies provide PHP and MySQL for a pretty cheap cost to the users.
So think of the possibilities that we have by using a dynamic scripting language like PHP and a stable open source database like MySQL. When I first learned PHP and MySQL my first thought was that by using PHP and a MySQL I could create a website that could contain thousands of pages but only three actual pages. So in terms of updating a Web Site design using PHP and MySQL would be a huge time saver.
With most web hosting where you have your hosting and MySQL databases together it makes it very easy to connect to the MySQL database to extract information or even insert information. So lets say that we have a database called "mysite" and the table in "mysite" called "mypages" and it is formated like the following:
ID | Title | Page |
So we have three fields in the table "mypages" which is in the database "mysite" and currently we do not have anything in this table it is just an empty table. So now we want to add something to this table using PHP and we will start at the beginning by connecting to the database and the table so we can INSERT information into the table.
First to connect to a MySQL database server we need to use the mysql_connection statement and give the server all the required information:
mysql_connect(server address, username, password);
Now we are going to save this statment to a variable so that we can use it over and over again.
$conn = mysql_connect('localhost', 'mike', 'mikenetpc');
Next we need to select the database within the server.
mysql_select_db('mysite');
So we have successfully connected to our database server and we have successfully selected the database we want to use. Lets now add some information to the table 'mypages' within the database 'mysite'. To do this we are going to use the mysql_query command along with the SQL INSERT command.
mysql_query("INSERT INTO mypages (ID, Title, Page) VALUES (1, 'Home Page', "This is my home page, Welcome')");
Now we have inserted a single row into the table 'mypages' lets add another one.
mysql_query("INSERT INTO mypages (ID, Title, Page) VALUES(2, 'About Page', ' This page is about me')");
We have successfully added two rows of information into our table but what good is this information if we can get it out of the table to display on a website. To extract the information from the table we will pull out all of the information and then create a loop to display it all. This is actually a lot easier than it my sound because we are going to extract the information we want into an array for easier handling. To get data from the table we will use the mysql_query command again and the SELECT command. I will write the entire piece of code and then I will explain it a little further down.
$result = mysql_query("SELECT * FROM mypages);
while($row = mysql_fetch_array($result))
{
echo $row['ID']."
";
echo $row['Title']."
";
echo $row['Page']."
";
echo "
;
}
We have already explained the first line, it SELECTS everything from the 'mypages' table of the database and places it into an array called $results. Next the create a while loop that will continue until we have reached the end of the array. Each time the loop occurs we move forward another line in the array so we will be displaying everything. Next we use the command echo which tells the server to display the information to the browser. So the statement echo $row['ID']."
"; will display the ID number of where we are in the array and then will display a
command to the web browser which is line break. The output of the above code would look like this.
1
Home Page
This is my home page, Welcom
2
About Page
This page is about me
So we have now successfully written to our database and we have successfully pulled information from our database. Those simple commands are the very basics of PHP and MySQL. Each of this commands has quite a few more attributes to them that you can use to sort all the information the table or even just pull out a single record from the table.
At the time of this article for $3.21 a month Cheap as Dirt Hosting offered web hosting with PHP, MySQL, EMail and MySQL database manager. You can probably see from this very basic example that you could create an entire dynamic website using information stored in a database. If you have and questions or need any help please feel free to post something here.
Mike Walton has been in the technology field for over 8 years and has 6+ years in hospitality technolgy. Mike has experience with Microsoft Windows Server 2000, 2003, Windows 98, XP, and Vista, Networking, Cisco Equipment, PCI DSS, and many more. Mike Walton is also the founder of MikeNet PC Free Articles and Videos. http://www.mikenetpc.com.
Article Source: http://EzineArticles.com/?expert=Mike_L_Walton