17
Basic Scripting & Variables Yasar Hussain Malik - NISTE

Basic Scripting & Variables Yasar Hussain Malik - NISTE

Embed Size (px)

DESCRIPTION

 Spaces, tabs, and new lines in between statements have no effect on how the code is executed. For example, this next script is the same as the previous script, even though it is laid out quite different:   It is generally recommended that you use whitespace to separate your code into clear blocks, so that it can almost be understood simply by visually inspecting the layout. like  $name = "Paul";  print "Your name is $name "; $name2 = $name;  $age = 20;

Citation preview

Page 1: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Basic Scripting&

Variables

Yasar Hussain Malik - NISTE

Page 2: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Whitespace Escape sequences Brief introduction to variable types Data Types Variable Functions Variable variables Pre-set variables References (pointer) Constants Mathematical Constants Operators The Ternary Operator Operator precedence and associability Variable Scope

Page 3: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Spaces, tabs, and new lines in between statements have no effect on how the code is executed.

For example, this next script is the same as the previous script, even though it is laid out quite different:

<?php    $name = "Paul"; print "Your name is $name\n";    $name2 = $name; $age = 20;

    print "Your name is $name2, and your age is $age\n";

    print 'Goodbye, $name!\n';?>

It is generally recommended that you use whitespace to separate your code into clear blocks, so that it can almost be understood simply by visually inspecting the layout. like

$name = "Paul"; print "Your name is $name\n";

$name2 = $name; $age = 20;

Page 4: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Escape sequences consist of a backslash followed by a single character. When enclosed in double quotes, the backslash causes the interpretation of the next character to "escape" from its normal ASCII code and to represent something else. To display the escape sequences in your browser, the HTML <pre> tag can be used; otherwise, the escape sequences placed within your PHP script will not be interpreted.

\‘ Single quotation mark \“ Double quotation \t Tab \n Newline \r Return/line feed \$ A literal dollar sign \\ Backslash \70 Represents the octal value \x05 Represents the hexadecimal character

Page 5: Basic Scripting & Variables Yasar Hussain Malik - NISTE

PHP supports four core data types:

•Integer •Float (also called double) •String •Boolean

In addition to the four core data types, there are four other special types:

•Null •Array •Object •Resources To see the type of a variable use function

$type=gettype(54.6);

Page 6: Basic Scripting & Variables Yasar Hussain Malik - NISTE

String literals are a row of characters enclosed in either double or single quotes.1 The quotes must be matched.

If a string of characters is enclosed in single quotes, the characters are treated literally (each of the characters represents itself).

Double quotes do not treat all characters equally. If a string is enclosed in double quotes, most of the characters represent themselves, but dollar signs and backslashes have a special meaning

Single quotes can hide double quotes, and double quotes can hide single quotes

"This is a string“ 'This is another string‘ "This is also 'a string‘“ 'This is "a string"‘ "5" is a string, whereas 5 is a number.

Page 7: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Boolean literals (introduced in PHP 4) are logical values that have only one of two values, true or false, both case insensitive.

You can think of the values as yes or no, on or off, or 1 or 0.

When using numeric comparison and equality operators, the value true evaluates to 1 and false evaluates to the empty

Page 8: Basic Scripting & Variables Yasar Hussain Malik - NISTE

NullNULL represents "no value," meaning "nothing," not even an empty string or zero. It is a type of NULL. An uninitialized variable contains the value NULL. A variable can be assigned the value NULL, and if a variable has been unset, it is considered to be NULL.

ResourceA resource is a special variable, holding a reference to an external resource such as a database object or file handler. Resources are created and used by special functions. File and database resources are defined by the PHP interpreter and are only accessible by functions provided by the interpreter.

The gettype() FunctionThe gettype() built-in function returns a string to identify the data type of its argument. The argument might be a variable, string, keyword, and so on. You can use the gettype() function to check whether or not a variable has been defined because if there is no value associated with the variable, the gettype() function returns NULL

Format string gettype ( mixed var )$type=gettype(54.6); // Returns "float" (e)

Page 9: Basic Scripting & Variables Yasar Hussain Malik - NISTE

To test whether a variable has a value set simply call the isset() function passing through the name of the variable as an argument . The following code example tests to see if a variable has a value or not and displays a message accordingly:

<?php $myVariable = "hello";   if (isset($myVariable)) { echo "It is set."; } else { echo "It is not set."; } ?>

Page 10: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Forcing a type with type casting PHP will automatically convert data types as

necessary across the board . If you specifically wish to override PHP's type

conversion, you can perform what is called a type cast

you forcibly convert a variable of type A to type B. In PHP, type casting looks like this:

<?php    $mystring = "wombat";    $myinteger = (integer)$mystring?>

Page 11: Basic Scripting & Variables Yasar Hussain Malik - NISTE

When you declare a variable outside a function, it may not be seen in the function. When you declare a variable inside a function, it may not be seen outside the function. That feature is called variable scope.

Page 12: Basic Scripting & Variables Yasar Hussain Malik - NISTE

If you find yourself setting a variable for convenience and never changing it during a script, chances are you should be using a constant. Constants are like variables except that once they are defined, they cannot be undefined or changed they are constant, as the name suggests. Unlike many other languages, constants are not faster than variables in PHP. The primary advantage to using constants is the fact that they do not have a dollar sign at the front and, therefore, are visibly different from variables. Furthermore, constants are automatically global across your entire script, unlike variables.

Page 13: Basic Scripting & Variables Yasar Hussain Malik - NISTE

You can define a constant by using the define()-function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined.

Only scalar data (Boolean , integer, float and string) can be contained in constants. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

You can get the value of a constant by simply specifying its name. Unlike with variables, a constant should not have a$ sign. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants

Page 14: Basic Scripting & Variables Yasar Hussain Malik - NISTE

These are the differences between constants and variables: Constants do not have a dollar sign ($) before them; Constants may only be defined using the define() function, not by simple

assignment; Constants may be defined and accessed anywhere without regard to variable

scoping rules; Constants may not be redefined or undefined once they have been set; and Constants may only evaluate to scalar values. Example Defining Constants <?php define("CONSTANT", "Hello world.");

echo CONSTANT; // outputs "Hello world."echo Constant; // outputs "Constant" and issues a notice.

// Defining Constants using the const keywordconst CONSTANT = 'Hello World'; echo CONSTANT;?>

Page 15: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Arithmatic Operator Description Example Result

+ Addition x=2, x+2 4

- Subtraction x=2, 5-x 3

* Multiplication x=4, x*5 20

/ Division 15/5, 5/2 3, 2.5

% Modulus (division remainder) 5%2, 10%8, 10%2 1, 2, 0

++ Increment x=5, x++ x=6

-- Decrement x=5, x-- x=4

Assignment Operator Example Is The Same As= x=y x=y

+= x+=y x=x+y

-= x-=y x=x-y

*= x*=y x=x*y

/= x/=y x=x/y

.= x.=y x=x.y

%= x%=y x=x%y

Page 16: Basic Scripting & Variables Yasar Hussain Malik - NISTE

Comparison Operator

Description Example

== is equal to 5==8 returns false

!= is not equal 5!=8 returns true

<> is not equal 5<>8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true

Logical Operator

Description Example

&& and x=6y=3 (x < 10 && y > 1) returns true

|| or x=6y=3 (x==5 || y==5) returns false

! not x=6y=3 !(x==y) returns true

Page 17: Basic Scripting & Variables Yasar Hussain Malik - NISTE

It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false. The ternary operator is a shorthand way of doing if statements. Here's an example:

<?php    $agestr = ($age < 16) ? 'child' : 'adult';?>