Sunday, 19 October 2014

PHP Basics #5 : PHP Data Types

PHP : Data Types


In this tutorial I will tell you about Data Types in PHP.



PHP : Data Types



There are mainly 7 types of data in PHP but we will cover only 5 types of data now and later on we will talk about the rest 2.
So the 5 data types which we are going to talk about now are the following..

  • String
  • Integers
  • Float Numbers
  • Boolean  
  • NULL


i. String



A string in php and all other programming or web developing languages are the alpha-numeric letters and sentences. In php we always have to write strings between single or double quotes.


<?php

echo "Hello I am a String";
echo "<br />";
echo 'Hello I am also a string between single quotes';

?>

The echo "<br />"; line in the code is an html tag for next line and this also shows us that we can also echo html tags if we want but for now dont use any other html tag except <br />.


If you output the above code in a browser the result will be this..

Hello I am a String
Hello I am also a string between single quotes  

That was all about String you must know till now..



ii. Integers (Numbers)



Integers in php and every other computer language represents all numbers. Integers in php does not require quotes around it.


<?php

echo 1;
echo '<br />';
echo 10000;
echo '<br />';
echo 99;

?> 

The output of the above codes in a browser will result..



1
1000
99

iii. Float Numbers



Floats are the numbers like the decimal numbers and thats all I can say about it you will understand better in the example below..


<?php

echo 1.09;
echo '<br />';
echo 0.89;
echo '<br />';
echo 40.6;

?>

If you open the above code in a browser the result will be the following..
1.09
0.89
40.6


iv. Boolean



Boolean is simply 'true' and 'false'. Boolean is mostly used in conditional coding and you will understand it when we start conditional coding in php. For now all you need to know is that boolean is either TRUE or FALSE.


<?php


//Thats how you declare them
$a = true;
$b = false;


?> 


v. NULL 



The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL.

The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases.

Variables can be emptied by setting the value to NULL


<?php

$a = 'Hello World!';
$a = null;

echo $a;

 ?>

The output of the above code will give you a blank page because  $a was 'Hello World!', but after that you again declared $a to NULL so $a has no value 

PHP Basics #4 : Variables

PHP Variables


As with algebra, PHP variables can be used to hold values (x=5) or expressions (z=x+y).

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for PHP variables:


  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case sensitive ($y and $Y are two different variables)

PHP Variables demonstration :

<?php

    $a = "John";
    $b = 'Terry';
    $x = 1;
    $y = 5;
    $z = $x + $y ;
    $float = 10.5;



echo $a;
echo '<br />';
echo $z;
echo '<br />';
echo $float;

?>


The output of the above will be:



John
6
10.5

Variables concept is quite easy to understand and If you have any conclusion in it please comment 

PHP Basics #3 : How to get Started with PHP coding ?

How to get started with PHP Development ?


(Continue only if you Successfully Installed Apache, PHP and MySQL)

So now you know how to run a php file in browser and its time to get started in php development !!

Today we will talk about :


  • How to write PHP codes
  • Basic Syntax
  • Echo Statement


1) How to write PHP codes ?


PHP codes can be written in any php file which means a file with .php extension and you can write it using any text editor but you must know that to write php codes you have to tell the browser that these are php codes not html and to do that all you have to do is open the php tag like all other html tags and then when php codes finishes close the php tag .. The opening php tag is <?php and the closing php tag is ?> . The browser will take all the codes between the <?php   ?> as PHP codes. And after you complete each php like you have to end with a semi-colon ; . A semi-colon tell the browser that the command ended. 


<?php

All codes here are PHP codes

?>  


2) Basic Syntax of PHP


PHP has very easy syntax rules as compared to other programming or scripting languages, the most important rule is to end your every statement with a semi-colon. We will demonstrate it soon and you will get hang of it.
One more important thing in php to remember is that white spaces had no effect in php coding there is no indentation rules in php, which for me is quite good.

3) echo 


If you have any experience with any programming languages then I guess you know what this statement does and for those who dont know this statement simply prints out anything you want on the screen. So if you have gone through the Last Tutorial of this series you must have created a folder in you localhost path named test and now time to use this folder for demonstration of all the exercises.
The syntax of echo statement is   
To print text in browser -> echo "Quotes if Text";
To print numbers in browser -> echo 1;
So create a new file named echo.php and inside the folder test and type the following inside the echo.php file..



<?php

echo "Hello World!!";


?>


If you open the file in browser the result will be..
Hello World!!


You can also do other things such as maths calculations..


<?php

echo 2+2;

?>

Result :
4

This was some basics of php codes and in next tutorials we will learn about PHP Variables and all..Till then do some experiments with your codes and try some more examples .. If you get stuck anywhere please comment

Tuesday, 14 October 2014

PHP Basics #1 : Introduction to PHP

Introduction To PHP


What is PHP ?
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML

What can PHP do ?

PHP is most commonly used as a Server-Side Scripting Language but it is not only bounded to it. PHP can also be used to write small desktop programs it is not that popular to creating desktop apps but still you can create small application which can help you in small tasks.

Why use PHP ?


1.) Works Great with HTML - If you already have a website and are familiar with HTML, making the step to PHP is easy. If fact, PHP and HTML are interchangeable within the page! While PHP might add some new features to your site, its basic appearance is still all created with HTML


2.) Interactive Features - PHP allows you to interact with your visitors in ways HTML alone can't. This can mean simple things like e-mail forms, or more elaborate things like shopping carts that save your past orders and recommend similar products. It can also mean social things like interactive forums and private messaging systems


3.) Easy to Learn - PHP is a lot easier to get started with than you might think. By learning just a few simple functions, you are able to do a lot of things with your website. And once you know the basics, there are a wealth of scripts available on the internet that you only need to tweak a little to fit your needs.


On the next post we will discuss about the Basic Syntax and how to write PHP codes on a html file..





PHP Basics #2 : How to Install Apache, PHP and MySQL

How to get started with PHP...?


Hello everyone in this tutorial I will show you how to make a development environment for PHP. This is one of the topics where most of the beginners have problems when to get to PHP development. You must get one thing clear that we cannot just open a editor and create a new html file and write php codes on it...this technique is not going to work with PHP as you need a localhost or a server PC to run php files and the file you are going to write PHP codes should be a .php file and don't worry this will not effect any of your html codes..

The above paragraph gives you alot of information but now lets get all topics clear.

To Start Developing in PHP :-
  • PHP will not work if we write php codes in an HTML file and open in the browser.
  • The file in which we will write php codes must be a PHP file ex. index.php and you dont need to worry about the HTML file getting corrupt or anything you HTML is gonna run fine in a php file.
  • We need a server to run php files. 
So the above three point is all we need to know for now and and we will solve each of them next..


Installation Guide For Ubuntu:


About LAMP

LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here is how to install the rest.


Step One—Install Apache



Apache is a free open source software which runs over 50% of the world’s web servers.
To install apache, open terminal and type in these commands:

sudo apt-get install apache2

That’s it. To check if Apache is installed, direct your browser to your server’s IP address (eg. http://12.34.56.789). The page should display the words “It works!" like this.
How to Find your Server’s IP address
You can run the following command to reveal your server’s IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'


Step Two—Install MySQL


MySQL is a powerful database management system used for organizing and retrieving data
To install MySQL, open terminal and type in these commands:

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.
Once you have installed MySQL, we should activate it with this command:


sudo mysql_install_db

Finish up by running the MySQL set up script:
sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.
Type it in.Enter current password for root (enter for none):
OK, successfully used password, moving on...
Then the prompt will ask you if you want to change the root password. Go ahead and give it a password for mysql
Once you're done with that you can finish up by installing PHP.


Step Three—Install PHP



PHP is an open source web scripting language that is widely use to build dynamic webpages.
To install PHP, open terminal and type in this command.


sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

After you answer yes to the prompt twice, PHP will install itself.
Congratulations! You now have LAMP stack on your computer!!



Test Your New Personal Server:

Now when you can open your browser and type on the url localhost/ and press enter. If the link opens an webpage saying Apache2 Ubuntu Default Page it means you have successfully Installed Apache Server on your machine ...!

Important Note: The localhost folder is located it /var/www/html/  now can can create a new folder test inside /var/www/html/ and then create a new file inside the test folder and name it index.php open that file with any text editor or your html editor and type the following :

<?php

echo "Hello World!!";

?>

Now goto browser and open localhost/test/index.php and see the result..!
(Dont worry I will explain the codes in next tutorial. If you encounter any problem please COMMENT.)

Saturday, 4 October 2014

What is sitemap and how to use it ?

What is a sitemap ?

sitemap is a file where you can list the webpages of your site to tell Google and other search engines about the organization of your site content. Search engine web crawlers like Googlebot read this file to more intelligently crawl your site.



Types Of Sitemap ?

There are two popular versions of a site map. An XML Sitemap is a structured format that a user doesn't need to see, but it tells the search engine about the pages in a site, their relative importance to each other, and how often they are updated. HTML sitemaps are designed for the user to help them find content on the page, and don't need to include each and every subpage. This helps visitors and search engine bots find pages on the site.

Sitemaps are written in XML (Extensible Markup Language) it is due to that the search engine crawlers can crawl easily an XML file. You should know that every directory can contain its own XML fine but its a good Web Developer's habit is to put the main sitemap.xml file on the root directory of the website. 


Here's an example of a sitemap.xml file :



sitemap.xml example



Below we will revise the lines of the sitemap file one by one:



  • Every Sitemap XML file must begin with an opening tag <urlset> and must end with </urlset>.
  • Every "parent" entry should begin with <url> tag and end with </url>.
  • In a similar way, every "child" entry should be placed between <loc> and </loc> tags. After a <loc> tag, an URL is expected which should start with "http://". The length of the URL can be 2048 characters at most.


  • The <lastmod> tag expects a date in the following format YYYY-MM-DD. Be advised that you do not have to modify this tag each time you modify the document. The search engines will get the dates of the documents once they crawl them.


  • The <changefreq> tag is used as a hint for the crawlers to indicate how ofter the page is modified and how often it should be indexed. Note that this value may or may not affect the crawl bot behavior which depends solely on the search engine.


  • The <changefreq> tag expects one of the following values: always, hourly, daily, weekly, monthly, yearly, never. Be advised that "always" is used for pages which are dynamically generated or changed/modified upon every access. As for the "never" value – be advised that even if you mark your page with a never value most probably it will be indexed once in a week for example.


  • The <priority> value can vary from 0.0 to 1.0. Be advised that this indicates only your personal preferences for the way you would like to have your website indexed. The default value of a page that is not prioritized is 0.5. Any page with higher value will be crawled before the page with priority 0.5, and all pages with lower priority will be indexed after the page with 0.5 value. Since the priority is relative it is used only for your website and even if you set a high priority to all of your pages this does not mean that they will be indexed more often, because this value is not used to make comparison between different websites.




Whats Next ?

I hope now you know what a sitemap of a website is and its tags also. But for those who dont want to write these all by themselves dont worry I have got a good website for you which will do these all automatically and then you can download the sitemap.xml file of your website and upload it on you root folder of your website.

Sitemap Generator



And Then...?

Well after you got the sitemap.xml file you can upload it on the root of your website you can goto Google WebMaster and submit your website's sitemap so that google bots can crawl it easily !!


How to submit..?

If you have uploaded the sitemap.xml file on your root folder of your website you can simply goto Crawl tab on the Google Webmaster and then sitemap and then click on Test/Add Sitemap and type your website url.

example: www.yourwebsite.com/sitemap.xml



I hope this was informative for you..Please Share