Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • difference between echo and print in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 106
    Comment on it

    Hello Readers,

    The below is the certain points which show the difference between echo and print in php:

    1. Echo and print both are the statements in PHP.

    2. Both are used to output data to the screen.

    3. In php echo is faster than print because both echo and print in php roughly same but using one over the other is not affect the improvement in your application.
    But if we say theoretically, echo is more efficient than print because it does not not return any value.

    4. In php echo has no return value while print has a return value of 1.

    5.Function vs Language Construct
    echo and print are not functions but use as a language constructs.

    Now there is a certain examples below which the practical difference between echo and print in php:

    First we explain the examples of echo in php:

    Examples of Echo:

    Eg1:

    <?php
    
    $name="Sachin";
    
    echo $name;
    //or
    
    echo ($name);
    
    ?>
    

    Output:
    Sachin

    In the above example:

    I have Creatde and initialize variable($name) holding a string value=Sachin. I want to print the name for this ($name) variable declared inside the echo with or without parentheses. it will display the same output.

    Eg2: (pass multiple argument)

    <?php
    
    $name = "Ram ";
    
    $profile = "PHP Developer";
    
    $age = 25;
    
    echo $name , $profile , $age, " years old";
    
    ?>
    

    Output:
    Ram PHP Developer25 years old

    In the above example:

    $name, $profile and $age are three variable with value=(ram , php developer and 25) respectively. Now we want to print all three variable values together. All variables names are define inside the echo statement separated by comma or dot(, or .)

    Eg3: (check return type)

    <?php
    
    $name = "Ravi ";
    
    $ret =  echo $name;
     ?>
    

    Output:
    Parse error: syntax error, unexpected T_ECHO

    In the above example:

    In this above program give the errot because echo has no return type in php so it gives the syntax error

    Now we explain the examples of print in php:

    Examples of print statement:

    Eg 1:

    <?php
    
    $name="Sam";
    
    print $name;
    
    //or
    
    print ($name);
    
    ?>
    

    Output
    Sam

    In the above example:

    Declare a variable ($name) value=Sam. now we want to print the name. we simply define $name inside print statement with or without parentheses it will show the output: Sam.

    Eg2 : (check return type)

    <?php
    
    $name = "Adam ";
    
    $ret =  print $name;
    
    //To test it returns or not
    echo $ret;
    
    ?>
    

    Output:
    Adam

    In the above example:

    declare a variable $name hold value=Adam.now we check the return type of print . So (print $name )is store in a variable($ret) . it will show $name value with return type=1.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: