Assigning Variable in PHP with Example

aimtocode

Definition of PHP

The PHP Stands for Hypertext Pre-processor. PHP is a programming language that allows web developers to create dynamic content that interacts with databases.


PHP Variable types

  • The main way to store information is by using a variable.
  • All variables in PHP are denoted with a leading dollar sign($).
  • The value of a variable is the value of its most recent assignment.
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  • Variables used before they are assigned have default values.

A variable in PHP is a name of memory location that holds data. A variable is a temporary storage that is used to store data temporarily.

PHP has a total of eight data types: Integers, Doubles, Booleans, NULL, Strings, Arrays, Objects and Resources(it is a special variable).

Syntax:


  $variablename=value; 

PHP variable Rules

  • PHP variables must start with letter or underscore only.
  • PHP variable can't be start with numbers and special symbols.

Example:




Result:


 string is: hello string
 integer is: 200
 float is: 44.6 

Variable Scope:

Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types:

PHP Constants

  • A constant is a name or an identifier for a simple value.
  • A constant value cannnot change during the execution of he script.
  • By default a constant is case-sensitive.
  • By convention, constant identifiers are always uppercase.
  • A constant name statrs with a letter or underscore, followed by any number of letters, numbers, or underscores.
  • To define a constant, use define() function and retrieve the value of a constant.
  • The function constant() is used to read a constant's value.

  • Syntax:



    Result:

      
      Welcome
    
    

    Pre-defined constants

    PHP provides a large number of predefined constants to any script which it runs.

    There are Basically five magical constants that change depending on where they are used.

    Name Description
    __LINE__ Represents current line number where it is used.
    __FILE__ Represents full path and file name of the file. If it is used inside an include, name of included file is returned.
    __FUNCTION__ Represents the function name where it is used. If it is used outside of any function, then it will return blank.
    __CLASS__ Represents the class name where it is used. If it is used outside of any function, then it will return blank.
    __METHOD__ Represents the name of the class method where it is used. The method name is returned as it was declared.

    _LINE_

    The current line number of the file where it is used.

    Example:_LINE_




    Result:

    
      The Line number : 3
    
    

    Example:_FILE_

    Represents full path and file name of the file. If it is used inside an include, name of included file is returned.




    Result:

    
      Your file name :D:\SQLBD\htdocs\php\index.php
    
    

    Example:_FUNCTION_

    _FUNCTION_ Represents the function name where it is used. If it is used outside of any function, then it will return blank.
    _METHOD_Represents the class name where it is used. If it is used outside of any function, then it will return blank.
    _CLASS_Represents the name of the class method where it is used. The method name is returned as it was declared.




    Result:

      Function of demo class : test
      Method of demo class : demo::testme
      Class : demo
    

aimtocode