I decided to also write a couple of articles describing the basics of php programming language. They are going to be very helpful for those of you who want to start development career, and especially the ones who want to do it fast.
Why php?
- Php is a powerful programming language that allows you to develop huge websites with tons of content. Php and MySQL go hand in hand.- Php is easy to learn. Compared to Java and C#, php is really a piece of cake to learn. In order to get really good in Java you will probably need years (if you are dedicated enough). And you could get good in php for just a couple of months.
- Php code is easy to maintain, update and tweak.
What do you need to run php scripts?
Ideally you are going to have hosting space on a server (with php installed) where you could upload and test your php files. Other option is to set up your own apache server with php. A good, reliable and fast alternative is to download XAMPP and use it for a portable server with php support and MySQL database.How to comment in php?
I am starting with comments before everything else, because I will be using a lot of examples and will comment them – just for you to get used to the code quickly. The comments syntaxis is like in C++ programming language and is simply done by using // or /* */, for example:// this is your comment going on just one row
or
/* this is your
comment going down
several rows */
How to initialize php?
The php code must be saved in php file by default and to initialize it you need to surround your code with either:<?php
// your code goes here
?>
or if the short tag is unable just
<?
// your code goes here
?>
I would highly recommend you to stick with the first option.
Php variables
To use a variable in php just use $(dollar sign) and then the name of the variable. Be careful because the name is case sensitive and should start with either a letter (A-z) or underscore (_). Php is great because you do not need to declare the type of the variable, to be able to use it (like in the other programming languages) – php will assign the most suitable type depending on the value of the variable. It is also important to remember that you need to assign value to the variable when first using it. Here are some examples:<?php
// “firstname” variable is assigned string value of “John”
$firstname = “John”;
// “firstnumber” variable is assigned integer value of “5”
$firstnumber = 5;
// “secondnumber” variable is assigned the float number 24.77
$secondnumber = 24.77;
// “a_bool1” is assigned a boolean value of true
$a_bool1 = true;
/* Defining arrays in php is a little bit tricky so I am only showing you the simplest ones below */
// Defining array of integer numbers
$arr_of_numbers1 = array(1, 2, 3, 4, 5, 100, 999, -300);
// Prints “1” on the screen
echo $arr_of_numbers1[0];
// Defining array of strings
$arr_of_colors1 = array('green', 'blue', 'white', 'black');
//Prints “white” on the screen
echo $arr_of_colors1[2];
// Closing php tag
?>
Two basic php functions
To conclude this lesson I am going to introduce you to the two most basic functions in php – “print” and “echo”. Both of them are used for printing a result on the screen.<?php
// Prints “I am learning php”
print "I am learning php";
// Will print 24.77 on the screen
print $secondnumber;
// Prints “I am learning php”
echo "I am learning php";
// Will print 5 on the screen
echo $firstnumber;
// Will print “The first number is 24.77 and the second number is 5”
echo “The first number is “,$secondnumber, “and the second number is “, $firstnumber;
// Closing php tag
?>
Great post indeed and thanks for all the information, it was very helpful i really like that you are providing information on PHP and MYSQL with basic JAVASCRIPT,being enrolled in
ReplyDeletehttp://www.wiziq.com/course/5871-php-mysql-with-basic-javascript-integrated-course
i was looking for such information online to assist me on php and mysql and your information helped me a lot. Thanks.