Showing posts with label PHPLearn. Show all posts
Showing posts with label PHPLearn. Show all posts

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 #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.)

Thursday, 14 August 2014

Learn Web Developing Languages For Free

Learn Web Developing Languages For Free


Hello everyone as I had planned to start teaching HTML, CSS, JavaScript, jQuery, PHP and MySql Basics which will help you to get a good foundation for you web developing skills. In these courses I will be making video tutorials as well as write about them with codes so that it will be easy for you to understand. In the following Learning Course I will teach you these web developing languages in an order i.e 
  1. HTML 
  2. CSS + Bootstrap3 (Basics)
  3. JavaScript
  4. jQuery
  5. PHP
  6. MySql
And remember that these courses will be the basics of them with the coding, don't worry after you complete the full course of all these languages you will be able to create a dynamic website with user login system with a database.

To get through this course all you need is a browser and a text editor, I will be using Ubuntu 14.04 and the text editor I will be using is Sublime-Text 3, you can use any text editor of your choice, but I recommend you the following text editors .

For Windows users Notepad ++ is a really good text editor as it has many good features and its easy to use but still you can use anyone of your own choice.

For Linux Users I recommend you to use Sublime-Text 3 with emmet plugin installed as it is really helpful you can google it to how to install sublime-text 3 and use emmet .

For Mac users you can also use Sublime-Text 3 or you can use TextMate too..  

You can ask me questions by comments or you can message me from the contract form.
Learning any type of programming or web developing language is easy but first few days you wont get hang of every thing but don't loose hope if you don't understand something see it again and again and withing 2 to 3 times it will be completely clear and if you wont understand even then, Message me and I will try to make you understand in a better way..

After every tutorial you can download the exercise files which will include all the html files I will be making in tutorials but first of all try to make it by yourself and them compare with mine so that you can find out if you made any mistake..

I will soon start uploading the course videos and write post on them..