PHP Data Types and Variable concepts

PHP Data Types and Variable concepts

PHP Data Types and Variable concepts




In my previous post, I introduced you all to PHP and the way to learn started with basic PHP scripts. This tutorial is in continuation to the previous post and it’ll look to clear up ideas of variables besides knowledge sorts in PHP.PHP Data Types and Variable concepts

php data variables and data types.
By the top of this text, you ought to be able to have deep insights into variable naming conventions, numeric, string, and array knowledge sorts and variable scopes in PHP.

PHP Data Types and Variable concepts

Read Post Here:

HOW TO BECOME A SUCCESSFUL BLOGGER : 18 STEPS TO FOLLOW

Variables in PHP

PHP Data Types and Variable concepts
PHP additionally has variables and constants a bit like different programming languages like C, Java, and BASIC. Variables area unit the storage areas wherever you’ll store values for later use. They act as identifiers to access memory locations.




In PHP, variable names begin with a dollar ($) sign, whereas assignment is applied by victimization “=” operator. what is more, all statements find yourself with a semi-colon (;) sign that indicates the termination of the statement. Another good thing about PHP is that we tend to don’t have to be compelled to specify the information sort victimization keywords like int, char, float, double or string.

Rules for Variable Declaration

  • A variable name should begin with a $ sign.
  • Variable name will ne’er begin with variety.
  • Variable will have associate degree underscore (_) sign or a letter.
  • Variable name could comprises alphanumerical characters (e.g: $firstname1, $_lastname2).
  • Variable names area unit case-sensitive (e.g: $lastname or $LASTNAME aren’t equivalent; each area unit the various variables).
  • You can assign variable names as short or as long as you would like (e.g: $a, $str, $firstname; all area unit valid).

PREMIUM BOOTSTRAP TEMPLATE/THEME IN FREE (LOW COST).

Example:

<?php

$a=5;

$a=$a+5;

echo “$a”;

?>

Output: 10


    • If a variable is appointed a worth over once, then it ought to be the foremost recent price.
    • In PHP, you don’t have to be compelled to declare knowledge sort before assignment.
    • White areas aren’t allowed in declaring PHP variable names.
    • In PHP, you don’t have to be compelled to grasp before whether or not you’ll store string or numbers.
    • PHP converts knowledge sorts itself once required.

    PHP Data Types and Variable concepts
    Examples of Variable Declarations

    PHP Data Types and Variable concepts

    6 BEST AD NETWORK (PPC ADVERTISING )FOR NEW BLOGGERS(WITH FAST APPROVAL)

    To understand the thought of variable higher, here we’ve got associate degree example.

    <?php

    $integer_num=10;    // Integer Value

    $float_num=22.3; // Float value

    $string_num=”Cloudways is a hosting service provider”;   //String value

    $Bool=false; // Boolean value

    $null_value=NULL; //Null declaration

    ?>

    Invalid Syntax

    Like in the other computing language, PHP has its share of invalid syntaxes.

    Example

    $2name=10; //Invalid because you can’t start a variable name with a number.

    $Permanent address=”7th street, Jackson Apartments, NYC”;  //Invalid address, can’t use space in a variable name declaration.

    $Personal-email=”david@gmail.com”; // invalid since operators are not allowed in variable declaration

    8 knowledge styles of PHP
    For constructing variables, PHP supports eight classes of knowledge sorts.

    1. Object

    PHP permits you to produce objects that comprise of functions and variables. Arrays and functions area unit samples of objects. we’ll be discussing this knowledge sort well after we cowl object-oriented programming in PHP. This knowledge sort not solely stores knowledge, however, it may record {the information|the knowledge|the knowledge} on the way to method data.

    Example

    <?php

    class webhosting {

    function webhosting() {

        $this->servicename = “cloudways”;

    }

    }

    // create an object

    $cloud = new webhosting();

    // show object properties

    echo $cloud->servicename;

    ?>

    Output

    cloudways

     

    2. Arrays

    PHP Data Types and Variable concepts
    An array is another kind of variable wherever you’ll store a group of values in a very single variable. Arrays area unit declared victimization the keyword array. There area unit three different types of arrays:

    Indexed arrays: They consists of a numeric index.
    Associative arrays: These arrays comprises named keys.
    Multi-dimensional arrays: These arrays comprises over one array.

    Example

    <?php

    $subject = array(“calculus”, “physics”, “chemistry”); // Creating an array $subject for storing subjects

    echo “I like ” . $subject[0] . “, ” . $subject[1] . ” and ” . $subject[2] . “.”;

    ?>

    Output

    I like calculus, physics and chemistry.

    3. Resources

    PHP Data Types and Variable concepts
    Resources area unit special variables dedicated for storing external resources of PHP; for e.g., connections of info. I will be able to deal with resources once covering advanced topics because it isn’t associate degree actual knowledge sort.

    32 BEST (AND FREE) WORDPRESS PLUGINS(UDATED 2018)

    4. Integers

    Integers area unit whole numbers while not a decimal point; e.g., 4195. they’re the only of the information sorts. Integers correspond to easy whole numbers, each positive and negative. Expressions may be utilized in the variable declaration.

    Example

    <?php

    $int_exp= 345-343;

    var_dump($int_exp);

    ?>

    Integers may be declared victimization positional notation, positional notation, and decimal format. By default, it’s in decimal format.

    5. Doubles

    All decimal containing numbers or floating-point numbers represent this knowledge sort, as an example, 34.4. It mechanically prints the minimum variety of decimal places required.

    Example

    <?php

    $a=44.234224;

    $b=5.4320998;

    $sum=$a+$b;

    echo($a+$b);

    ?>

    Output

    49.6663238




    6. Boolean

    Boolean knowledge sort could contain 2 prospects, either True or False.

    Example

    <?php

    $a=true;

    if(true)

    Echo “Statement is valid”.”<br/>”;

    Else

    Echo “Invalid statement”.”<br/>”;

    var_dump($a); // This function returns the data type of the variable.

    ?>

    Output

    Statement is valid

    bool(true)

    • Rules of mathematician knowledge sort
    • Value of variable is fake if it equals to zero; otherwise, true.
    • It is false even once the string is empty or zero. Else, it’ll stay true.
    • Null sort values area unit invariably thought of as false.
    • An array containing no values is false; otherwise, true.
    • It is prohibited to use floating purpose numbers as Booleans.

    PHP Data Types and Variable concept

    4 AMAZING TIPS TO SIMPLIFY YOUR WEB DESIGN

    7. Null

    It is one special knowledge sort whose price is usually NULL. Such variables that area unit appointed NULL are literally treated as false.

    Example

    <?php

    $empty= null;

    var_dump($empty)

    ?>

    Output

    NULL

    8. Strings

    Strings area unit a set of characters which will have any text inside quotes. you will use single or quotation mark. Object and array area unit are known as “compound knowledge types”, whereas the opposite 5 be the class of “simple knowledge types”.

    In PHP, there’s no limit with relation to the string length. Escape Character sequences that contain backlashes area unit replaced with special characters.

    String Character Replacements

    Example

    <?php

    $a=”Welcome to Shahrookhblog”;

    echo $a;

    ?>

    Output

    Welcome to Shahrookhblog

    Following area unit the main points of string character replacements:

    /” are going to be replaced by one double quote.

    PHP Data Types and Variable concepts
    /$ are going to be replaced by $ sign itself.

    PHP Data Types and Variable conceptsPHP Data Types and Variable concepts
    /t is going to be replaced by a tab character.

    PHP Data Types and Variable concepts
    /r is going to be replaced by printing operation character.

    PHP Data Types and Variable concepts
    /n is going to be replaced by printing operation character.

    AMAZING SEO GUIDE FOR BEGINNERS ( 2018 UPDATED )

    PHP Data Types and Variable concepts

    PHP Variables Scope

    PHP Data Types and Variable concepts
    PHP has four totally different variable scopes.

    1. Parameter

    PPHP Data Types and Variable conceptsHP Data Types and Variable concepts
    It is a neighborhood variable whose price is lapsed business code through a preform. This knowledge sort desires declaration inside a parameter list.

    Example

    <?php

    Function cloudways($hosting)

    {

    echo “$hosting”;

    }

    cloudways(34);

    ?>

    Output

    34




    PHP Data Types and Variable concepts
    2. Static

    When it’s needed to stay native variable values instead of being deleted, then we tend to use a static keyword for declaring the variable.

    Example

    <?php

    function var_scope()

    {

    Static $var=3;

    Echo $var;

    $var–;

    }

    var_scope();

    var_scope();

    var_scope();

    ?>

    Output

    321

    PHP Data Types and Variable conceptsPHP Data Types and Variable concepts
    3. Global

    PHP Data Types and Variable concepts
    Global variables area unit variables that area unit outlined outside the performing. These variables can’t be used from inside the perform and need the employment of world keyword. It permits their use inside a perform.

    Example

    <?php

    $s = 155;

    $t = 20;

    function varscope()

    {

    global $s, $t;

    $t = $s + $t;

    }

    varscope();

    echo $t;

    Output

    175

    4. Local

    PHP Data Types and Variable concepts
    Variables declared inside a perform have, by default, native scope and may solely be accessed inside the performing. Their values area unit deleted once variables area unit outside the preform.

    Example

    <?php

    Function local_Var()

    {

    $g=4;

    Echo $g;

    }

    local_Var();

    ?>

    Output

    4

    PHP Data Types and Variable concepts
    4. Super international Variables

    PHP Data Types and Variable concepts
    PHP supports associate degree array of world variables. These arrays area unit known as superglobals. they’re accessible from anyplace within the script and don’t have to be compelled to be declared victimization the keyword international.

    Following is that the list of world variables:

    $_GET: It contains info about the shape field sent by the browser victimization GET.
    $_POST: It contains info about the shape field sent by the browser victimization POST
    $GLOBALS: It contains a list of world variables, except the superglobals.
    $_FILES: It contains info concerning files to be uploaded.
    $_REQUEST: It consists of combined values control by $_COOKIE, $_GET, $_POST.
    $_COOKIE: It holds cookies sent by the browser.
    $_SESSION: This array permits retrieval of session variables from the browser.
    $_SERVER: This array holds informatics address and server connection information.
    $_ENV: This array consists of surroundings variables passed to PHP. They embrace CGI variables and variables provided by the shell.

    PHP Data Types and Variable concepts
    This concludes the PHP knowledge sorts and variable declarations tutorials. I hope you were able to get some sensible learning out of this. If you’ve got any longer queries, then be at liberty to question me within the comments below and that I would be glad to guide you.

    In the next tutorial, I will be able to be covering twelve basic PHP string functions that each beginner ought to lear


    n. keep tuned for it!

    Please follow and like us:
    0

    Related posts

    Leave a Comment