WINTER Template Web Development PHP Variables and Constants Lecture-3
Lecture Content What is Variable? Naming Convention & Scope PHP $ and $$ Variables PHP Constants Constant Definition Magic Constants Examples & Coding Practice
What is Variable? Variable is nothing but just a name of the memory location. In simple words, it is a container which is used to store both numeric and non-numeric information. Naming conventions- Variables start with a $, followed by variable name. value VaraibleName Variable name must start with letter or underscore. Variable name can only contain alpha-numeric characters and underscores. Ex- (A-Z, 0-9 and _) Space is not allowed. Variable names are case-sensitive ($age and $AGE are two different variables) A variable name cannot start with a number
Declare Variable A variable is created the moment you assign the value to it. Example- <?php $ number1 = 5; $ number2 = 4; echo Sum=.$ number1 + $ number2;?> Output: Sum= 9
Variable Scope Variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local global Static
Global Keyword The global keyword is used to access a global variable from within a function. To do this, use the global keyword before the variables (inside the function). <?php $number1 = 5; $number2 = 10; function myfunction() { global $ number1, $ number2, $sum; $sum = $ number1 + $ number2; } myfunction (); echo $sum;?> Output 15 Note- PHP also stores all global variables in an array called $GLOBALS[index].
PHP $ and $$ Variables $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc. $$var (double dollar) is a reference variable that stores the value of the $variable inside it. Example <?php name 500 var name $var = "name"; $$ var = 500; echo $var."<br/>"; echo $$ var."<br/>"; echo $ name; Output: name 500 500?>
PHP Constants PHP constants are name or identifier that can't be changed during the execution of the script. Differences between constants and variables- There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign. Constants cannot be defined by simple assignment, they may only be defined using the define() function. Constants may be defined and accessed anywhere without regard to variable scoping rules. Once the Constants have been set, may not be redefined or undefined.
PHP Constants PHP constants can be defined by 2 ways: 1. function define() 2. Constant() PHP constants follow the same PHP variable rules. Constants are automatically global and can be used across the entire script.
PHP Constants PHP constant: define() Let's see the syntax of define() function in PHP. 1. define(name, value, case-insensitive) 2. name: specifies the constant name 3. value: specifies the constant value 4. case-insensitive: Default value is false. It means it is case sensitive by default. Example <?php define("firstmessage","hello Learning Veda"); echo FirstMessage;?> Output: Hello Learning Veda
PHP Constants Constant() function Constant function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function. Example <?php define("minsize", 50); echo MINSIZE; echo constant("minsize"); // same thing as the previous line?> Output: 50 Note- Only scalar data (boolean, integer, float and string) can be contained in constants.
Magic Constants Name LINE FILE DIR Description Represents current line number where it is used. Represents full path and file name of the file. If it is used inside an include, name of included file is returned. Represents full directory path of the file. Equivalent to dirname( file ). It does not have a trailing slash unless it is a root directory. It also resolves symbolic link. FUNCTION CLASS TRAIT Represents the function name where it is used. If it is used outside of any function, then it will return blank. Represents the function name where it is used. If it is used outside of any function, then it will return blank. Represents the trait name where it is used. If it is used outside of any function, then it will return blank. It includes namespace it was declared in. METHOD NAMESPACE Represents the name of the class method where it is used. The method name is returned as it was declared. Represents the name of the current namespace.
Thanks!! You can find many more Courses on my website