As we all know every language whether it be a scripting language or a programming language has variables in it. php which is a server-side HTML embedded scripting language also has variables which help it to store values anytime it wants it to.
The variables in php are very easy to use and they need not be declared to specific type like int,float,String etc, we just have to assign a value. This is because they automatically convert to the data-type we require. So there's no need to declare them in a different way each time, or create a value for them each time.
The variables has to start with a $ sign and then a name for the variable. The name cannot start with a number, it can either start with an underscore (_) or a letter. No other special characters are allowed except underscores, letters and numbers, as long as it doesn't start with a number.
The line termination is done with the help of of a semi-colon (;).
Lets take a look at an Example and understand it better :
<?php
$topic = "php tutorial on Variables";
$number = 1;
echo "Code 2 Learn Post on $topic part $number";
?>
In the above example we have taken 2 variable namely topic and number. the value in topic is in " " because we need to make it a String type and the value in number is not in " " because we wanted number to be integer. We can also put 1.5 or any other value and the datatype will accordingly change itself.
The echo" " is used to print the output onto the screen of the user.
And also the thing with variable is the they are easy to concatenate. In fact, probably, concatenation is the wrong word – they're very easy to include inside your string. Here we have done just that, i.e. we have included the value of $topic and $number.
The output of the above code would be :
This is all you need to know in variables. There are many types of variable like boolean, float etc, but you would have to declare them in the same way as we did above.
So practice this and you can come back and learn some more advanced functionality later on, when I'm going through some other projects.
There is no decimal type in PHP. There are exactly 8 types:
ReplyDeleteboolean
integer
float
string
null
array
resource
object
http://php.net/manual/en/language.types.php
@linepogl thank you for the comment..
ReplyDelete