Search This Blog

Friday, January 8, 2016

echo and print in PHP

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

  1. echo is a statement i.e used to display the output. it can be used with parentheses echo or without parentheses echo.
  2. echo can pass multiple string separated as ( , )
  3. echo doesn’t return any value
  4. echo is faster then print
Eg i
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)
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)
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

  1. Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
  2. using print can doesn’t pass multiple argument
  3. print always return 1
  4. it is slower than echo
Eg i
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)
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)
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.

PHP Magic Constant

PHP Magic Constant

There are a number of predefined constants available to your scripts.
We will use these constants as we need them; there’s a sample:

PHP: Magic Constant

PHP Magic Constant
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file.
__FUNCTION__ The function name
__CLASS__ The class name
__METHOD__ The class method name
PHP_VERSION The PHP version
PHP_INT_MAX The PHP integer value limit

__LINE__

The current line number of the file.
Output
The Line number : 2

__FILE__

The full path and filename of the file.
Output
Your file name : C:xampplitehtdocsmagic_constantfile.php

__FUNCTION__,     __CLASS__,     __METHOD__

The function name
The class name
The class method name
Output
Function of demo class : test
Method of demo class : demo::testme
Class : demo

PHP_VERSION

The PHP version
Output
Current PHP Version you are using : 5.3.1

PHP_INT_MAX

The PHP integer value limit
Output
Integer Maximum Value : 2147483647

Constant in PHP

Constant in PHP

  1. Constants are PHP container that remain constant and never change
  2. Constants are used for data that is unchanged at multiple place within our program.
  3. Variables are temporary storage while Constants are permanent.
  4. Use Constants for values that remain fixed and referenced multiple times.

Rules for defining constant

  1. Constants are defined using PHP’s define( ) function, which accepts two arguments:
    The name of the constant, and its value.
  2. Constant name must follow the same rules as variable names, with one exception the “$” prefix is not required for constant names.
  3. Syntax:

    Valid and Invalid Constant declaration:


    Create a constant and assign your name

    Output
    Hello Rexx
    In the above example
    We define a constant using define( ) function. first argument for name of constant and second for its value=”phptpoint”.
    Now we print the value. Pass name of constant inside print statement
    Output will become

    Sum of two numbers using constant

    Output
    Sum of two constant = 200
    In the above example
    We declare three constant of name=(ONE,TWO,SUM). First Add the value of two constant.
    Now sum of these two value work as a value for third define constant(SUM).
    Now pass $sum inside print it will show the sum of two number.

    Subtraction of two numbers using constant

    Ex.iii
    Output
    Subtraction of given number = 500
    In the above example
    We define three constant with name(X,Y,Z). First subtract the value of two defined constant .
    Now result of these two value work as a value for third define constant(Z).
    Pass Z inside print so it will show the subtraction of two value.

    Sum of two numbers and assign the result in a variable

    Output
    Sum of two constant = 200
    In the above example
    We define two constant with name(one,two) and value(100,100) respectively.
    $res variable is also define.
    Now we perform addition of two defined constant value and result store in a variable($res = one+two;).
    To print the result pass $res inside print statement.

    Building a Dollars-to-Euros Converter

    Output

    USD/EUR Currency Conversion

    150 USD is equivalent to :120 EUR
    if you have been following along, the script should be fairly easy to understand.
    It begins by defining a constant named “EXCHANGE_RATE” which surprise, surprise stores the dollar-to-euro exchange rate(assumed here at 1.00 USD to 0.80 EUR).
    Next, it defines a variable named “$dollars” to hold the number of dollars to be converted, and then it performs an arithmetic operation using the * operator, the “$dollars” variable, and the “EXCHANGE_RATE” constant to return the equivalent number of euros.
    This result is then stored in a new variable named “$euros” and printed to the Web page.
 

Super Global Variables in PHP

Super Global Variables in PHP

PHP super global variable is used to access global variables from anywhere in the PHP script.
PHP Super global variables is accessible inside the same page that defines it, as
well as outside the page.
while local variable’s scope is within the page that defines it.
The PHP super global variables are :
$_GET[“FormElementName”] : It is used to collect value from a form(HTML script) sent with method=’get’.
information sent from a form with the method=’get’ is visible to everyone(it display on the browser URL bar).
$_POST[“FormElementName”] : It is used to collect value in a form with method=”post”>. Information sent from a form is invisible to others.(can check on address bar)
$_REQUEST[“FormElementName”] : This can be used to collect data with both post and get method.
$_FILES[“FormElementName”] : It can be used to upload files from a client computer/system to a server.
OR
$_FILES[“FormElementName”][“ArrayIndex”] : Such as File Name, File Type, File Size, File temporary name.
$_SESSION[“VariableName”] : A session variable is used to store information about a single user, and are available to all pages within one application.
$_COOKIE[“VariableName”] : A cookie is used to identify a user. cookie is a small file that the server embedded on user computer.
$_SERVER[“ConstantName”] : $_SERVER holds information about headers, paths, and script locations.
eg. $_SERVER[“SERVER_PORT”], $_SERVER[“SERVER_NAME”],$_SERVER[“REQUEST_URI”]