Saturday, June 30, 2018

Difference between echo and print in PHP

PHP echo and print both are PHP Statement.

Both are used to display the output in PHP.

echo
  • echo is a statement i.e used to display the output. it can be used with parentheses echo or without parentheses echo. 
  • echo can pass multiple string separated as ( , ) 
  • echo doesn’t return any value 
  • echo is faster then print 
Eg i


<?php

$name="Ravi";

echo $name;

//or

echo ($name);

?>

Output
Ravi

In the above example
Create and initialize variable($name) hold a string value=”Ravi”. we want to print the name for this ($name) variable declare inside the echo with or without parentheses .
it will display the same output.

Eg ii (pass multiple argument)

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

Output
Ravi PHP Developer25 years old

In the above example
$name , $profile and $age are three variable with value=(“ravi” , “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 comm or dot(, or .)
it will show the output

Eg iii (check return type)

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

Output
Parse error: syntax error, unexpected T_ECHO

In the above example
In this program we check the return type of “echo”. declare a variable $name with value=”ravi”.
now we check the return type. when we run the program it show error,because echo has no return type.

Print

Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
using print can doesn’t pass multiple argument
print always return 1
it is slower than echo

Eg i

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

Output
Ravi
In the above example
Declare a variable ($name) value=”ravi”. now we want to print the name. we simply define $name inside print statement with or without parentheses.
it will show the output: “ravi” .

Eg ii (pass multiple argument)

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

Output
Parse error: syntax error
In the above example
Declare three variable $name, $profile, $age and hold the value(“ravi”,”php developer”,25).
now check whether it will allow execute multiple argument. Pass three variable inside the print statement separated by comma. as we run this program it show some error.
it means multiple argument are not allow in print .

Eg iii (check return type)


1
2
3
4
<?php
$name = "Ravi ";
$ret =  print $name;
//To test it returns or not
echo $ret;
?>


Output
Ravi
In the above example
declare a variable $name hold value=”ravi”.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.

For More Info Clickhere:

Exercise 1



/*
   Step 1:  Use the Echo Function to say hello with html h1 tags embedded inside php.


   Step 2: Write a comment above the echo function and explain
   what that function is doing.


   */



============

<?php include "functions.php" ?>
<?php include "includes/header.php" ?>

<section class="content">

<aside class="col-xs-4">

<?php Navigation();?>


</aside><!--SIDEBAR-->


<article class="main-content col-xs-8">



<?php

    // this functions says Hello
    echo "<h1> Hello</h1>";

/* 
   Step 1:  Use the Echo Function to say hello with html h1 tags embedded inside php.


   Step 2: Write a comment above the echo function and explain
   what that function is doing.


   */

?>



</article><!--MAIN CONTENT-->

<?php include "includes/footer.php" ?>

Using Dynamic Data


Using Dynamic Data we can change all h1 tag name in single time..


<!DOCTYPE html>
<html>
<body>



<?php
 $title = "Hellow World"; //delaring variable
?>


<h1> <?php echo $title  ?> </h1> 
</body>
</html>

Comments in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.
Comments can be used to:
  • Let others understand what you are doing
  • Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
PHP supports several ways of commenting:

Example

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/


// You can also use comments to leave out parts of a code line$x = 5 /* + 15 */ + 5;
echo $x;
?>


</body>
</html>
Run example »

Types of Errors in PHP

Basically there are four kinds of errors in PHP, which are as follows:
  • Parse Error (Syntax Error): The parse error occurs if there is a syntax mistake in the script; the output is Parse error. A parse error stops the execution of the script. There are many reasons for the occurrence of parse errors in PHP. The common reasons for parse errors are as follows:
  1. Unclosed quotes
  2. Missing or Extra parentheses
  3. Unclosed braces
  4. Missing semicolon
Example:
<?php
echo "Cat";
echo "Dog"
echo "Lion";
?>
Output:
In the above code we missed the semicolon in the second line. When that happens there will be a parse or syntax error which stops execution of the script, as in the following image:
  • Fatal Error: Fatal errors are caused when PHP understands what you've written, however what you're asking it to do can't be done. Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.
Example:
<?php
function fun1()
{
echo "Vineet Saini";
}
fun2();
echo "Fatal Error !!";
?>
Output:
In the above code we defined a function fun1 but we call another function fun2 i.e. func2 is not defined. So a fatal error will be produced that stops the execution of the script. Like as in the following image.
  • Warning Error: Warning errors will not stop execution of the script. The main reason for warning error is to include a missing file or using the incorrect number of parameters in a function.
Example:
<?php
echo "Warning Error!!";
include ("Welcome.php");
?>
Output:
In the above code we include a welcome.php file, however the welcome.php file does not exist in the directory. So there will be a warning error produced but that does not stop the execution of the script i.e. you will see a message Warning Error!! Like in the following image:
  • Notice Error: Notice error is the same as a warning error i.e. in the notice error, execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.
Example:
<?php
$a="Vineet kumar saini";
echo "Notice Error !!";
echo $b;
?>
Output:
In the above code we defined a variable which named $a. But we call another variable i.e. $b, which is not defined. So there will be a notice error produced but execution of the script does not stop, you will see a message Notice Error!! Like in the following image:
What are the different types of errors in PHP?
Basically there are 13 types of errors in PHP, which are as follows:
  1. E_ERROR: A fatal error that causes script termination
  2. E_WARNING: Run-time warning that does not cause script termination
  3. E_PARSE: Compile time parse error
  4. E_NOTICE: Run time notice caused due to error in code
  5. E_CORE_ERROR: Fatal errors that occur during PHP’s initial startup (installation)
  6. E_CORE_WARNING: Warnings that occur during PHP’s initial startup
  7. E_COMPILE_ERROR: Fatal compile-time errors indication problem with script
  8. E_USER_ERROR: User-generated error message
  9. E_USER_WARNING: User-generated warning message
  10. E_USER_NOTICE: User-generated notice message
  11. E_STRICT: Run-time notices
  12. E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
  13. E_ALL: Catches all errors and warnings

Output Functions in PHP



Output functions in php are used to display some text on browser.

Types of output functions are:

1.Print:

The print statement can be used with or without parentheses: print or print().

Display Text


Example

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Run example »
Display Variables

The following example shows how to output text and variables with the print statement:

Example

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
Run example »



The following example shows how to output text with the print command (notice that the text can contain HTML markup):

This function displays output on browser and returns a boolean value.

Note: The value true is returned if the output is printed sucessfully else it returns false.In php true value is 1 and false doesn't contain any value.We cannot use print function to display multiple functions at once.

<?php

$rval=print("scott");

print $rval;

?>

2.Echo:


The echo statement can be used with or without parentheses: echo or echo().
Display Text
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):

Example

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Run example »
Display Variables
The following example shows how to output text and variables with the echo statement:

Example

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Run example »



It is same as print function but doesn't return any value.Hence, it is faster than print function.we can print multiple statements with this function.

<?php

echo("Scott","John","Smith");

?>

3.Var_dump:This function displays variable value along with its data type.

<?php

$sno=123;

$un="Smith";

var_dump($sno); //displays int(123)

var_dump($un); //string(5) "Smith"

?>

4.Print_r:By using this function wecan display all elements of array and properties of objects.Before learning about this print_r function we should have some basic understanding about arrays.

Array:Every array in php is contribution of 'KEY' and 'VALUE".The default key starts from zero.

$arr=array(10,20,30);

Since php is loosely typed language,we can store collection of different datatypes in arrays.The difference between normal variable and array isthat a normal variable can store single value whereas an array can store multiple values.

<?php

$arr=array(10,20,30);

print_r($arr); //prints all array elements

echo $arr[1]; //20

?>

In the above example the line print_r($arr); prints all array elements in the 'KEY' and 'VALUE' style.

Output:Array([0]=>10[1]=>20[2]=>30).

For More Info: Clickhere

DECLARATION STYLE TAGS OF PHP


Types of declaration style tags:
1.Universal style tag: Supports all functionalities of php.
               <?php
                  echo("universal style tag"):
               ?>
2.Short open tag: Supports limited functionalities of php.

                <?
                 echo("short open tag");
                ?>
3.ASP style tag: We can also include the php script within the ASP declaration syntax.
               <%
                  echo("asp style tag");
               %>
4.Script style tag: This is the oldest style tag used for all types of scripts.
                <script language="php">
                   echo("script style tag");
                </script>
Sometimes we don’t get the output of ASP style tag. If output of ASP style tag is not displayed on the browser screen,then follow these steps:
1.Open the php.ini.
2.Find asp_tags = off.
3.Replace off with on.
4.Save the new settings.

5.Now run the code of ASP style tag.We get the desired output.
So php.ini is very useful in chaning the desired configuration settings.

Installation of PHP



After install XAMPP, we can find out XAMPP folder & it contains subfolders…

htdocs: it is root directory of apache server. All php programs we should save at this location

tmp: it is temporary memory location of server. All uploaded files and sessions will store at this location.

Php: this folder contains PHP configuration settings. php.ini is configuration setting of PHP.

Apache:…..

Mysql:……

Why PHP? or Features of PHP ?



· PHP is Open Source

· PHP is free. Download it from the official PHP resource: www.php.net

· PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

· PHP is compatible with almost all servers used today (Apache, IIS, etc.)

· PHP supports a wide range of databases

· PHP is easy to learn and runs efficiently on the server side

· PHP Supports different Types of Content managment systems like Joomla, Drupal & Wordpress..

· PHP Excute very fast becuase of Zend Engine

What is PHP?


  • ·         PHP is an acronym for "PHP: Hypertext Preprocessor"
  • ·         PHP is a widely-used, open source scripting language
  • ·         PHP scripts are executed on the server
  • ·         PHP is free to download and use
  • ·         PHP is an amazing and popular language!

Presently Used in:
  • ·         It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)!
  • ·         It is deep enough to run the largest social network (Facebook)!


What is a PHP File?
  • ·         PHP files can contain text, HTML, CSS, JavaScript, and PHP code
  • ·         PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • ·         PHP files have extension ".php"


What Can PHP Do?
  • ·         PHP can generate dynamic page content
  • ·         PHP can create, open, read, write, delete, and close files on the server
  • ·         PHP can collect form data
  • ·         PHP can send and receive cookies
  • ·         PHP can add, delete, modify data in your database
  • ·         PHP can be used to control user-access
  • ·         PHP can encrypt data
  • With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.


PHP Prerequisites



· HTML

· CSS

· Javascript

PHP Overview

Overview:


PHP is a server side scripting language, and a powerful tool for making dynamic and interactive Web pages.

Implemented by Rasmus Lerdorf in 1995 using c & perl Languages

PHP stands for Personal Home Page and also its contains it alies name i.e Hypertext preprocessor