Variables in php

Hello again, today i decided to write some basic concepts about php as a programming language and the first thing i want to talk about are the variables.

First thing that you should know is that when you declare a variable you need to put the dollar ( $ ) sign before your variable ex: $x;

Assuming you know what a variable is, in php, when you declare a variable, you don’t need to declare it as an int, float, double, string and so on, php will automatically know what kind of data type your variable is for example:

In C# you declare a variable of type int like so:

int x = 10;

In php you declare a variable of type int like so:

$x = 10;

Data type that php supports are:

  1. String
  2. Integer
  3. Float ( floating point numbers: 3.15 )
  4. Boolean
  5. Array
  6. Object
  7. Null
  8. Resource

You can find some more information about variables in php here.

Now let’s make a simple exercise with variables and the echo function that it was presented in the Your first php script post:

Inside your index.php script let’s declare a string variable and name it message that will be equal with a random message of your choice, my message will be “This is a message stored inside a variable” and after that let’s output that message into the browser.

<?php

   $message = “This is a message stored inside a variable”;

    echo $message;

?>

After you write the code save it and refresh the page in your browser, if everything went fine you will see your message written in the browser.

Another  type of exercise that you can do is to declare two variables of type int ( whole numbers ) and assign them two different numbers. After you did that, declare a third variable of type int that it will be equal with the sum of the other two variables and echo the output like we did it it the first example. If you do this leave me comment with your result or questions if you can’t handle it and i will respond and i will try to explain to you personally the solution.

Cheers

 

 

Leave a comment