In my last document we discussed about how to install apache server, PHP in a server. And how to print a statement in a web page. Let's have an overview about PHP.
PHP is an Object Oriented Programming Language. (That means it allows work with classes and its instance). We will discuss about O.O.P in great detail in future blogs. And most of the time PHP compiles line by line (for your basic understanding ). As we discussed in our last lesson PHP server reads whatever inside <?php ?> (or if you configured your server to allow short tags then <??>) and executes it.
Lets have an example,
But you will see some thing like this in your web page.<?php echo 'Hello World'; // outputs Hello World echo 'This is second line'; //outputs This is second line echo 3+4; // outputs 7 ?>
Why? because there is no line breaks. The corrected version will be ,Hello WorldThis is second line7
Lets assign values to some variables. As the way PHP is designed , it has no casting or variable type at all. Sure there is , but not when you are assigning values to it. You can assign any value including Class objects, Arrays , Strings , Numerics , Lists etc., etc. Lets have an example.<?php echo 'Hello World <br/>'; // outputs Hello World echo 'This is second line<br/>'; //outputs This is second lineecho 3+4; // outputs 7 ?>
As you can see we can assign any type to a variable. There are two naming conventions to variables. Of course you cannot declare a variable starting with a numeric. For declaring a variable you should start with Dollar Sign '$'. Then continue the name that you can understand easily. If the variable name is one word then you can put it as what it is. As $mango . If it is more than one word, either you can separate the word with a UNDERSCORE '_' or you can continue all words together by capitalizing first character of each word except initial word. Lets have an example.<?php $my_first_variable = 'Hello World'; // assigns Hello World to $my_first_variable $my_second_variable = 3; $my_third_variable = 4; $my_array = array(1,2,3.1,3,'Tithira Hiranjth','I love dogs'); $my_class_object = new myClass(); ?>
- $this_is_first_convention
- $thisIsSecondConvention
We concatenate strings by using dot ".". Lets have an example.
Now we'll do some simple math.$myCountry = 'I am '.' Sri Lankan'; echo $myCountry; //this will print I am Sri Lankan.
Thank you! Let's meet again with next lesson Conditional Statements.$first_math= $my_first_variable + $my_second_variable;// this will assign 3+4 = 7 echo $first_math; //this will print 7.
the easy book to learn PHP
ReplyDeleteClick To Download