Thursday, March 12, 2009

PHP Variables

What is variables?

A variable is a mean to store values such as strings or integers so we can easily reuse those values in our code.

Example

<?php
$str="Welcome to Php";
//here is one variable with a string value
echo $str;
//here we print the $str value onto the browser
$num = 100;
//another variable with a integer value
echo $num;
// here we echo the $num variable to print its value
echo $str;
//here we reuse the $str variable to print its value again in our code
echo $num;
//here we reuse the $num variable to print its value again in our code
?>


The following code should print "Welcome to php100
Welcome to php100". Try it.Copy the code in an empty
file,save the file as php and run it on your browser.

Variables in PHP

Every language has its own semantics of defining
variables.In PHP, we define a variable starting
with a $ sign,then write the variable name
and finally assign a value to it.


Here is an example below.

$variable_name = value;

You must add the $ sign when declaring variables,
otherwise it will not work. It is advisable that when
you define a variable, you must initilaize it by
assign it a value.


Working With PHP Variables
Few Naming Rules
There are few things we need to note when working with PHP variables.
  • A variable name should only start with underscore "_" or a letter.
  • A variable can only contain letters, numbers or underscore. In another words, you can't use other funky characters like <^# in your variables names.
Declaring a PHP Variable

Let say we want to store values using variables in PHP. Here's how we do it.
  • First, think of a logical name for a variable. For example, if you we want to store the text of your company logo in a variable, the logical name for that variable would be something like $logo_text. Remember, you can name your variables whatever you like. But you must choose logical names so it is easier to remember what they are
  • Assign that variable a value.
  • Remember to put the $ sign in front of the variable name.
Example
PHP String Variable
Example - PHP variable with a string value
<?php
$logo_text = "Welcome to php";
echo $logo_text;
?>


In the above code we define a variable name $logo_text,
assign it a string value of "
Welcome to php"and print
its value onto the page.


PHP Integer Variable

Example - PHP variable with an integer value

<?php
$number = 100;
echo $number;
?>

In the above code we define a variable name $number,
assign it an integer value of 100 and print its value
onto the page.


PHP Float or Decimal Variable

Example - PHP variable with a float or decimal value

<?php
$decimal_number = 100.01;
printf("%.2f", $decimal_number);
?>


In the above code we define a variable name
$decimal_number, assign it a decimal value of 100.01 and
print its value onto the page.

points to when declaring variables in PHP
  • 1.Remember to always put $ sign in front of variable names. That tells PHP that we're working with a variable.
  • 2.In PHP a variable name must start with a letter or an underscore followed by combination of letters, number or underscores.
  • 3.PHP variables are case sensitive, that means they could be either lower case or upper case or combination of both.
  • 4.You don't have to worry about declaring the type of the variable. For example strings and integers are declared the same way.
  • 5.It's good practice to initialize a variable i.e. assign it a value. Uninitialized variables in PHP have a value of either false, empty string or empty array.

Monday, March 9, 2009

PHP Syntax

PHP Tags


PHP is a web scripting language which means it was desgined to work with HTML. can easily embed PHP code into your HTML code. When writting php code, you enclose your PHP code in special php tags. This tells the browser that we're working with PHP.

<?php
//your php code goes here
?>>


PHP and HTML

<html>
<title>my php page</title>
<body>
<?php
echo "hello there!";
?>
</body>
</html>


Echo is a special statement in php for outputing data to the browser. This statement is more often used when
we need to print something to the browser.

My First PHP Page

  1. Open up notepad.
  2. Copy paste the above PHP and HTML code in notepad.
  3. Now, lets save the file in our www folder, i.e. c:/wamp/www
  4. Name the file my_php_page.php and save it in your www folder.
  5. Now, go to http://localhost/my_php_page.php in your web browser.
Example - Make Text Bold

<?php
echo "<b>hello there!</b>";
?>

Output

hello there

Installing PHP on Windows

Installing PHP is easy. You can install PHP on your computer easily with WAMP5. WAMP5 is an open source application which comes with with PHP5, Apache web server, MySQL database and phpMyAdmin (mysql database administration program). You can install these items on your windows machine using WAMP5. You can read more about WAMP5 at www.wampserver.com.

Installing WAMP5


  1. Download the latest version of WAMP5.
  2. Run the installation wizard and follow the easy instructions to install WAMP5.
  3. Once you're done installing WAMP5, please go to your folder where you installed wamp5. If you didn’t specify which folder to install wamp during the installation wizard, then it should be installed in your C:/ drive, i.e. C:/wamp.
  4. In your wamp directory you should see the following folders.
    1. apache2 – This is where the apache web server is installed. Apache will run our php code and scripts on the web browser. You will not need to do anything in this folder. So you can relax and ignore it for now:)
    2. mysql – This is where MySql databse is installed. MySQL is an open source database. PHP and MySQL work really well together. You can use PHP and MySql to store data. Don’t worry, we will learn how to do all that in our tutorials.
    3. php – You guessed it. This is where php modules are stored.
    4. www – This is the root folder of our web server. This is where you are going to put all your php files and scripts.
    5. wampserver.exe – This file will run the WAMP program. We need to start WAMP everytime we want to work with PHP. So, go ahead and double click on this file.
  5. Once you run the wamp server file, you will see a small icon on your windows tray.
Right click on this icon and then click on “Start All Services”. This will start the apache web server along with everything we need to run PHP pages on our machine.

    That it! You’re almost done.
6.Now open up your web browser and type in http://localhost. You should see the WAMP welcome page.

When you type in http://localhost in your browser, it executes the index.php file in your www folder. All our php files will go in the www folder.

Introduction to PHP

What is PHP?

PHP stands for "PHP: HyperText Preprocessor". PHP is a server side scripting language for making logic driven websites. Ever wonder how they made that "contact us" form on their site, which sends out emails? Well, they used PHP. Or, how they made that image upload tool? Well, they used PHP. PHP written scripts can use databases to keep track of your customer's and visitors activities on your site, send out periodical newsletters to your subscribers, upload files or images and drive the content on your site dynamically. The possibilities are endless. Most of the social networking websites you visit are writing in PHP. PHP is that powerful. Learning The Basics of PHP will help you tremendously in your Webpage development.

How PHP Works?

PHP sits between your browser and the web server. When you type in the URL of a PHP website in your browser, your browser sends out a request to the web server. The web server then calls the PHP script on that page. The PHP module executes the script, which then sends out the result in the form of HTML back to your browser, which you see on the screen. Here is a basic php diagram which illustrate the process.

PHP Process Diagram


What are PHP benefits?

PHP is a free open source language. That means you don't have to pay thousands of dollars in licensing fee to acquire PHP. Best of all, it is easy to install. The most striking feature of it is that it is easy to learn. PHP is used by millions of people and developers around the world. There are thousands of websites on the internet which are written using PHP. One primary example is Yahoo! Bookmarks. PHP has an ever growing community. There are thousands of websites, forums and community websites dedicated on PHP. I built this site to share my PHP examples, recourses and code with you that you can use on your website easily. I will also show you how to build your own PHP website with all the bells and whistles from ground up. You will also learn the fundamentals of PHP and write your own PHP code. Basic PHP at the ground level up.

Seo Basics

What is Search Engine Optimization (SEO)?


Search Engine Optimization (SEO) is the collection of techniques used to make a Website or Web page more visible in search engines like Google and thus increase the number of visitors to the site. Most of Search Engine Optimization is properly an engineering discipline and not "marketing." However, aspects of search engine optimization like getting people to link to your pages, using e-mail newsletters to popularize your site and writing attractive content require marketing skills. The real goal of Search Engine Optimization is not to get high positions in search engines for your pages. The real goal is to get more visitors to your website.

Why do you need Search Engine Optimization (SEO)?

Traffic from search engines should constitute the major source of visitors to your Website. Whether you are selling a product or just putting your thoughts on the Web, you probably want people to see them.

Basic Principles of Search Engine Optimization


The idea of SEO is to tell the search engines that a page or site exists, that it contains certain content that visitors want to read, and that other Web sites or Web pages trust the information in that page. The two basic elements of SEO are the Keywords (a "keyword" is a word or phrase that people search for in search engines) that tell search engines and their visitors what is in the page, and the links that tell search engines the page exists and is trusted (has Web site Authority)and also help tell them what keywords are in it.

Goal of Search Engine Optimization (SEO)

The goal of SEO is to get high positions in search engine results for popular keywords, especially in Google, the most popular search engine. Studies show that visitors will click on the first 10 links retrieved by search engines, especially the first two or three, and ignore all the rest. If you are selling widgets, people find your site by searching for the word widgets in the search engine. If your site is at the top of the listings, you will get a lot of visitors - if a lot of people are looking for widgets.

What do you need to do for SEO?

All of Search Engine Optimization can be divided into three parts:

On Page Optimization - Keywords and coding and content on a page.

Off Page Optimization - This usually refers to Links from other Websites.

Website SEO design - Link structure in your site,

Type of software used to create your Web pages,

Size of your site.

The main things that need to be done for Search Engine Optimization:

Selecting the right software platform for new site.

Designing the structure of the site properly.

Selecting the right keywords - Keywords that visitors are searching for, for which there are not too many existing pages, and that represent your site or article theme. Those keywords are your Nichehe.

tag in the code (See SEO in HTML Code, the headings of topics, and bottom of the page, and especially, of course in the text. On Page Optimization - Putting those keywords in the text and in "strategic" places in the page such as:

The page title tag in the code (See SEO in HTML Code , the headings of topics, and bottom of the page, and especially, of course in the text).

Using the keywords in Links to the page or site, both inside your own website and in links from directories and other Web sites.

Exchanging Links with other Web sites.

Search Engine Optimization can be done by everyone. Even if you do not know anything about HTML code, you can do a great deal to improve the visibility of your Web site in search engines. See SEO for Everyone. You can also hire an SEO consultant, but you should know enough about the subject so you can judge if you are getting fair service.