Php – Variables

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-variables/
PINTEREST
PINTEREST
INSTAGRAM

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations

Variables are used to store data in application. Variables can store data like string, text, integer, float number, logic value etc. Variables may defined different ways but there are some restrictions for variables names. Here some important things about variable naming.

$ Sign

All variables start with $ sign. It is like variable selector. Following example show valid and invalid variable named.

Valid

  • $age
  • $test1
  • $NAME
  • $SiZe

Invalid

  • age
  • a$age
  • $test$
  • $9name

Variable Name Must Start with Letter or Underscore

Variable names can not start other than letter or underscore. If this is not obeyed will create fatal error.

Valid

  • $_age
  • $Test
  • $My_Test

Invalid

  • $.test
  • $-age

Variable Name Cannot Start with Number

Variables can not start with numbers. This is one of the pitfalls new developers or learner fall.

Valid

  • $age1
  • $T1st

Invalid

  • $1
  • $1Test
  • $2age2

Variable Name Contain Alphanumeric Characters

Variables can only contain alphanumeric characters. Alphanumeric characters consist of A-z , 0-9 , _ .

Variable Name Cannot Contain Spaces

Variable names cannot contain spaces. This will break up the name convention.

LEARN MORE  Linux Bash How to Define, Use Shell Variables

Valid

  • $my_test
  • $yourtest

Invalid

  • $my test
  • $test your

Variable Types

As stated before variable may have different types. These type determination is done while assigning value to the variable. In the following example we assign an integer value 9 to the variable named $age and this will set the type of the $age as integer automatically.

Php have following primitive variable types where we will look all of them in the next chapters.

  • String
  • Integer
  • Float
  • Double
  • Boolean
  • Null

Variable Scope

Variables can be defined in different part of the code or in different files. What will happen if the same variable name defined in different files where these files are important current file. This will create some conflict about scope. Should we access all variables from current file or restrict the scope. We will also look variable scopes in next chapters.

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-variables/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)