Friday, November 7, 2014

Learning PHP (Variables) - part 3

Hello again

I am back with another article of series learning php. This time around we will be talking about the variables in php. Unlike many other languages like C or java we don't have to specify variable type in php. All we do is create a variable which can be referred to as a container for storing information. The syntax to create a variable is



<?php
      $myvariable;
?>

That's right. It's as simple as writing a dollar sign and specifying name of variable. You can assign any value to a variable in php, doesn't matter if it's string, boolean, integer or double. All it will od is store it and ofcousre php knows what kind of value is stored in it and it'll treat that value accordingly. That's all you need to know about variables in php

Now we will talk a little about specific type of values stored in php variables and handling them e.g., add, subtract, append, copy etc.,

Mostly following kind of values exist when we talk about php variables.
  1. String
  2. Integers
  3. Floating point numbers
  4. booleans
  5. Arrays
  6. Objects
  7. null values
You can store any kind of text in a php variable by just enclosing it in single or double quotes e.g.,

<?php
        $mystring= 'hello! I am a string variable';
        echo $mystring;
?>

Similarly you can store integers, floating point numbers, and other mentioned values upto number four in a variable e.g.,

<?php
    $myinteger= 1000;
    $myfloat= 1.3245;
    $mybool= true;
    $mynull = null;
    echo $myinteger.$myfloat.$mybool.$mynull;
You can add the values to be displayed in echo function by using a dot ".".

Declaring an array in php is slightly different. You can make an array by doing

<?php
     $myarray= array('val1', 'val2', 'val3');
      print_r($my_array);
?>

Above example contains a string array. There are associative arrays in php too. We will talk about associative arrays, more than one dimensional arrays and objects in coming posts. Hopefully in next few posts we will make a small calendar app, which will allow users to append specific events to different dates, and we will also learn about mysql a little.

No comments:

Post a Comment