How to Declare Variables in PHP - Informatic Point

How to Declare Variables in PHP

How to declare a variable in PHP. The following article, variables in PHP, gives an outline for the different variables available in PHP. All variable stores some set of information where information is a value. This value of information can be a string, boolean, number, array or it can be an object, and so on.

Declaration of variables in PHP?

The variables declared reserve information. There are several things one necessity to know about representing variables in PHP.

  • Variables represented always begin with a dollar sign ($). A variable name must start each with a letter or an underscore but not with a number.
  • Variables do not carry spaces and these variable names are case-sensitive example $apple is different from $Apple.
  • Variables declaration uses the assignment operator ‘=’ wherein the variable name is on the left side of the operator and the appearance on the right side of the operator.
  • As we understand that PHP is a loosely standardized language, the variables represented do know in advance what type of the variable it will be, meaning that it can be represented as a number or a string or an array or anything else.

How to Initialize Variables in PHP?

From the earlier, we know that PHP language is a loosely typed language and we need not represent the type like whether the variable is of integer, or string or boolean type before using it as it appears in other languages. The type of variable depends upon the value of its repositories. Let us learn through variables in PHP with examples.

Here in the following example, we observe that the height is a float value and the base is an integer value and based on these values we have estimated the area of the triangle.

<?php
$height = 15.5;           //float variable
$base = 40;               //integer variable
$area_of_triangle = ($height * $base) / 2;
echo 'The area of triangle is '. $area_of_triangle;
?>
Output
The area of triangle is:310

Types of Variables with Examples

Variables store the values. These values allocated to the variables specify what type of variable it is. There are seven data types listed below:

  1. Integer
  2. String
  3. Array
  4. Boolean
  5. Float
  6. NULL
  7. Object

1. Integer

An integer is a complete number. This integer can be positive or negative. It urgently has at smallest one digit ordering from 0 to 9, with no comma or blanks. It does not have a decimal point. Integers have different notations like

  1. decimal(base 10)
  2. hexadecimal(base 16, prefixed with 0x)
  3. octal(base 8, prefixed with 0)

optionally preceded with a sign either – or +

<?php
$x = 700;
$y = 35;
var_dump($x);
echo '<br>';
var_dump($y);
?>
Output
int(700)
int(35)

2. String

A string is a flow of characters or letters. A string can contain a flow of numbers of arithmetic values also. It can be a sequence of all as well. To represent a string we use single or double quotes.

<?php
$input = 'Grape ';
echo '<br> $input is my favorite fruit';
echo "<br> $input is my favorite fruit";
?>
Output
$input is my favorite fruit
Grape is my favorite fruit

3. Array

It is a set of similar and different data types. An array is represented in the form of a key-value pair.

<?php
$directions= array('Apple','Orange','Banana','grape');
var_dump($directions);
echo '<br>';
echo $directions[2] echo '<br>';
echo $directions[0];
?>

4. Boolean

This data type can contain one of two values either a true or a false, where true is 1 and false is blank.

<?php
$input = true;
echo "<br> True is ".$input;
$input_value = false;
echo "<br> False is ".$input_value;
?>
Output
True is 1
False is

5. Float

A number with a decimal point or using an exponential form is called a floating-point number or is of type float.

<?php
$input = 10.45;
$input_value = 5.e5;
var_dump($input);
echo '<br>';
var_dump($input_value);
?>
Output
float(10.45)
float(500000)

6. NULL

When no value is specified to a variable and the variable is empty we can use NULL value.

<?php
$input = 'The output of demo';
var_dump($input);
echo '<br/>';
$input = NULL;
var_dump($input);
?>
Output
string(18) “The output of demo”
NULL

7. Object

An object is a data type that stores data. Besides data, it also stores information about the processing of the data. An object is represented explicitly by declaring class variables in PHP. The class is specified with the class PHP keyword. A Class is a structure that holds data members and data methods.

A class is instantiated and the object is formed, and through this object now we can reach the members and methods of the class.

<?php
class sub{
public $string = "My favourite sub is Computer";
function display() {
return $this->string;
}
}
$object = new sub;
echo $object->string;
?>
Output
My favourite sub is Computer

27 thoughts on “How to Declare Variables in PHP

  1. This is really interesting, You’re a very skilled blogger.
    I’ve joined your feed and look forward to seeking more
    of your wonderful post. Also, I’ve shared your web
    site in my social networks!

  2. I was very happy to uncover this web site. I need to to thank you for your time just for
    this wonderful read!! I definitely loved every little
    bit of it and i also have you saved to fav to look at new things in your web site.

  3. Fastidious answers in return of this difficulty with solid arguments and
    describing everything about that.

  4. Hello to every single one, it’sreally a fastidious for me to go
    to see this web site, it includes useful Information.

  5. Wow, marvelous blog layout! How long have you been blogging for?

    you made blogging look easy. The overall look of your website is wonderful, as well as the content!

  6. Hello there, I discovered your blog by wway of Google whilst searching for
    a related subject, your website got here up, it appears great.

    I have bookmarked it in my google bookmarks.
    Hello there, just was aware of your blog thru Google, and found that it’s truly
    informative. I am going to watch out for brussels. I’ll appreciate when you
    proceed this in future. Many folks might be benefited
    frkm your writing. Cheers!

  7. Good day! I could have sworn I’ve been to this website before but after looking at many of the articles
    I realized it’s new to me. Anyhow, I’m certainly happy
    I stumbled upon it and I’ll be bookmarking it and
    checking back often!

  8. It is the best time to make some plans for the future and it’s time
    to be happy. I’ve read this post and if I could I want to
    suggest you some interesting things or advice.
    Maybe you can write next articles referring to this article.
    I want to read even more things about it!

  9. It is not my first time to pay a visit this web site, i am browsing this website dailoly and get nice information from here all the time.

  10. Quality articles or reviews is the secret to be a focus for the users to visit the web
    page, that’s what this web site is providing.

  11. you’re actually a just right webmaster. The website
    loading pace is amazing. It kind of feels that you’re doing any unique trick.

    In addition, The contents are masterpiece. you have done a excellent job on this matter!

  12. Hi, all the time i used to check blog posts here in the early hours in the break
    of day, as i like to learn more and more.

  13. Thank you for sharing your thoughts. I truly appreciate your efforts
    and I will be waiting for your further write ups thanks once again.

  14. Нeya і’m for the first tіme һere. I
    came across this board and І find Ιt reaⅼly useful & it helped me out a lot.
    І hope to ցive something back and aid ⲟtherѕ like you aided me.

  15. If you are going for best contents like me, just pay a quick visit this web
    site daily since it gives feature contents, thanks

  16. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the
    message home a little bit, but other than that, this
    is fantastic blog. An excellent read. I will certainly be back.

  17. I every time used to read piece of writing in news papers but now
    as I am a user oof internet so from now I am using net for content, thanks
    to web.

  18. These are genuinely impressive ideas in on the topic of blogging.
    Y᧐u have touched some pleasant fаctors here. Any way keep up wгinting.

  19. Thanks for sharing your info. I truly appreciate your efforts
    and I will be waiting for your next post thanks once again.

  20. This is a topic that’s near to my heart… Thank you!
    Exactly where are your contact details though?

  21. I very time սsed to study paragraph іn ness papers
    but now as I аm a user ⲟf neet ѕo from noѡ Ι аm usiing net fⲟr articles, thɑnks to web.

Leave a Reply

Your email address will not be published. Required fields are marked *