payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Rules for naming Variables and Constants   in Category: PHP   by amit

🕙 Posted on 2023-04-25 at 19:26:58     Read in Hindi ...


Fundamentals of Programming

    Every programming language has some reserved keywords and built-in functions. These built-in functions according to their behaviour, are classified into functions, methods, properties, constructs, etc. All these technical terms have their specific meaning.

Variables and Constants

    As explained in previous page, Variables and Constants are names of memory (RAM) locations, which refer to respective memory locations of values stored in them. It seems confusing, but different types of variables and constants have been allocated required memory for storing different types of DATA. When you access these variables and constants by their names, they will provide you respective DATA (value).

    Imagine variables and constants as POST-CARDs, if you have purchased one! POST-CARDs are hard-paper (size: 14cm × 9cm), sold at POST-OFFICES. In a POST-CARD, one side you have half portion available for writing name and address of receiver (NAME of variable or constant). Other half and back side portion is used for writing content of your letter (VALUE of variable or constant).

Rules for Naming Variables:

    Variables are different from Constants, therefore, there are different rules for nomenclature of variables and constants. In a request-response cycle, the value of a variable can be changed, but the value of a constant will not be changed. These specification are made intentionally, so that programmers can use them as per their requirements.

    In PHP, a variable name must start with $ (dollar) symbol. In JavaScript also, you can name a variable with $ symbol, but it is your choice. In Python, C, C++, etc. variable names cannot start with $ symbol.

    In all these programming languages, variable names must be started with _ (underscore) or English alphabets (from A to Z and a to z). Thus, in PHP, after the $ symbol, you must have at least one English alphabet or _ (underscore) symbol. For example, $my_name, $My_Name, $_my_name

    A variable name cannot start with numbers, even after the $ symbol in PHP. This will show warning and your program will stop execution of CODES. For example, $1name will display ERROR.

    A variable name must not contain any special character, or blank space within it. A blank space separates one word from another, and therefore, PHP will interpret one as variable and another as constant. For example, $my@name or $my name

    A variable name must not be exactly same as those of reserved keywords. Reserved keywords in a programming language, are meant for proper running of all CODES. Therefore, you should avoid one, two, three underscores just after $ symbol. For examples, $_my_name , $__my_name , $___my_name , etc. are valid variable names, but they create confusion even to the programmer who name them!

    You should either use lowercase letters with _ (underscore) symbol, or use caMelcAse pattern for naming variables. For example, $myName

    A variable name can contain numbers after the second position, that is, after $ and starting letter or underscore. For example, $_1name But, you should have meaningful names of variables and constants.

Variable names are case sensitive.

    You should name variable and constant carefully, because both are CASE SENSITIVE. It means that $myname is different name from $Myname and both refer to two different memory locations.

Rules for Naming Constants:

    A constant name in PHP must not start with $ symbol. It means every word which is left without (not enclosed by) a pair of single quotes or a pair of double quotes or, as explained above, when there is a blank space between a variable name, could be treated as constants.

    There are some reserved keywords, which are without (not enclosed by) a pair single quotes or double quotes, have special meaning. These are: TRUE true FALSE false NULL null string int integer float bool boolean array , etc. You will later learn more about constants and how to use them.

A constant name can be defined in two ways:

    define("MY_NAME", "Mickey");

    const MYNAME = "Mickey";

    Caution: Remember that a constant name cannot have different values in one request-response cycle, that is, you cannot re-define it. Therefore, you can see in above two examples that first constant name is MY_NAME and the second constant name is MYNAME. Both constant names can have same value, but these values will be stored in different memory locations.

Reserved Keywords

abstract and array() as break callable case catch class clone const continue declare default die() do echo else elseif empty() enddeclare endfor endforeach endif endswitch endwhile eval() exit() extends final finally fn for foreach function global goto if implements include include_once instanceof insteadof interface isset() list() match namespace new or print private protected public readonly require require_once return static switch throw trait try unset() use var while xor yield yield from __halt_compiler()

Compile-time constants: __CLASS__   __DIR__   __FILE__   __FUNCTION__   __LINE__   __METHOD__   __NAMESPACE__   __TRAIT__

    Now, with the help of these reserved keywords, you can script your program, and create dynamic WEB-PAGES. For example, you can write following first line of CODE in a file with .php extension.

<?php echo 123; ?>

    You will learn more about everything mentioned in above line of CODE, in next page. Some of you may recognize what these are, but beginners will not be able to run this CODE without proper guidance.


Leave a Comment: