PHP DAY 3 (Comments,Variables,Variables Scope,Data Types,Super Global variables)

DAY - 3

Comments in PHP

Ø  A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.
Ø  PHP supports several ways of commenting:

Ø  Example

// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

 

 


PHP Variables

Ø  A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Ø  Rules for PHP variables:
·         A variable starts with the $ sign, followed by the name of the variable
·         A variable name must start with a letter or the underscore character
·         A variable name cannot start with a number
·         A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
·         Variable names are case-sensitive ($age and $AGE are two different variables)
Note:
Ø  PHP variable names are case-sensitive! In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.

Ø  Statement in PHP is any expression that is followed by a semicolon (;).


PHP Variables Scope

In PHP, 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 - A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function
  • Global - A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function
·         Static -  Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable


PHP Data Types

Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
·        Integers − are whole numbers, without a decimal point, like 4195.
·        Doubles − are floating-point numbers, like 3.14159 or 49.1.
·        Booleans − have only two possible values either true or false.
·        NULL − is a special type that only has one value: NULL.
·        Strings − are sequences of characters, like 'PHP supports string operations.'
·        Arrays − are named and indexed collections of other values.
·        Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
·        Resources − are special variables that hold references to resources external to PHP (such as database connections).
Note: The first five are simple types, and the next two (arrays and objects) are compound - the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.

 

Super Global variables

·         Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

·          

The PHP superglobal variables are:

·         $GLOBALS
·         $_SERVER
·         $_REQUEST
·         $_POST
·         $_GET
·         $_FILES
·         $_ENV
·         $_COOKIE
·         $_SESSION

1 comment:

Give your valuable feedback

Topic :Software & Types, Subject: Computer Fundamental Notes for CSJM University Kanpur(for different courses like BBA, BCA, etc..)

Software Software refers to the programs, data, and instructions that enable a computer or other digital device to perform specific tasks or...