Tuesday 3 January 2017

Learn PHP 2

 

PHP: Loosely typed language

PHP is a loosely typed language, it means PHP automatically converts the variable to its correct data type.

PHP Constants

PHP constants are name or identifier that can't be changed during the execution of the script. PHP constants can be defined by 2 ways:
  1. Using define() function
  2. Using const keyword
PHP constants follow the same PHP variable rules. For example, it can be started with letter or underscore only.
Conventionally, PHP constants should be defined in uppercase letters.

PHP constant: define()

Let's see the syntax of define() function in PHP.
1.    define(name, value, case-insensitive)  
  1. name: specifies the constant name
  2. value: specifies the constant value
  3. case-insensitive: Default value is false. It means it is case sensitive by default.
Let's see the example to define PHP constant using define().

File: constant1.php

1.            <?php  
2.            define("MESSAGE","Hello Jsk Suresh  PHP");  
3.            echo MESSAGE;  
4.            ?>  
Output:

Hello Jsk Suresh  PHP

 
File: constant2.php

1.            <?php  
2.            define("MESSAGE","Hello Jsk Suresh PHP",true);//not case sensitive  
3.            echo MESSAGE;  
4.            echo message;  
5.            ?>  

Output:

Hello Jsk Suresh PHPHello Jsk Suresh PHP
 
File: constant3.php


1.            <?php  
2.            define("MESSAGE","Hello Jsk Suresh PHP",false);//case sensitive  
3.            echo MESSAGE;  
4.            echo message;  
5.            ?> 

Output:

Hello Jsk Suresh PHP
Notice: Use of undefined constant message - assumed 'message' 
in C:\wamp\www\vconstant3.php on line 4
message

PHP constant: const keyword

The const keyword defines constants at compile time. It is a language construct not a function.
It is bit faster than define().
It is always case sensitive.

File: constant4.php

1.            <?php  
2.            const MESSAGE="Hello const by Jsk Suresh PHP";  
3.            echo MESSAGE;  
4.            ?>  

Output:

Hello const by Jsk Suresh PHP

PHP Data Types

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
  1. Scalar Types
  2. Compound Types
  3. Special Types

PHP Data Types: Scalar Types

There are 4 scalar data types in PHP.
  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types

There are 2 compound data types in PHP.
  1. array
  2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.
  1. resource
  2. NULL

PHP Operators

PHP Operator is a symbol i.e used to perform operations on operands. For example:

Operators
Additional Information
Associativity
clone new
clone and new
non-associative
[
array()
left
**
arithmetic
right
++ -- ~ (int) (float) (string) (array) (object) (bool) @
increment/decrement and types
right
instanceof
types
non-associative
!
logical (negation)
right
* / %
arithmetic
left
+ - .
arithmetic and string concatenation
left
<< >>
bitwise (shift)
left
< <= > >=
comparison
non-associative
== != === !== <>
comparison
non-associative
&
bitwise AND
left
^
bitwise XOR
left
|
bitwise OR
left
&&
logical AND
left
||
logical OR
left
?:
ternary
left
= += -= *= **= /= .= %= &= |= ^= <<= >>= =>
assignment
right
and
logical
left
xor
logical
left
or
logical
left
,
many uses (comma)
left



     1.             $num=10+20;//+ is the operator and 10,20 are operands  
In the above example, + is the binary + operator, 10 and 20 are operands and $num is variable.

PHP Operators can be categorized in following forms:

  • Arithmetic Operators
  • Comparison Operators
  • Bitwise Operators
  • Logical Operators
  • String Operators
  • Incrementing/Decrementing Operators
  • Array Operators
  • Type Operators
  • Execution Operators
  • Error Control Operators
  • Assignment Operators
We can also categorize operators on behalf of operands. They can be categorized in 3 forms:
  • Unary Operators: works on single operands such as ++, -- etc.
  • Binary Operators: works on two operands such as binary +, -, *, / etc.
  • Ternary Operators: works on three operands such as "?:".

PHP Operators Precedence

Let's see the precedence of PHP operators with associativity.

PHP Comments

PHP comments can be used to describe any line of code so that other developer can understand the code easily. It can also be used to hide any code.
PHP supports single line and multi line comments. These comments are similar to C/C++ and Perl style (Unix shell style) comments.

PHP Single Line Comments

There are two ways to use single line comments in PHP.
  • // (C++ style single line comment)
  • # (Unix Shell style single line comment)
1.            <?php  
2.            // this is C++ style single line comment  
3.            # this is Unix Shell style single line comment  
4.            echo "Welcome to PHP single line comments";  
5.            ?>  

Output:

Welcome to PHP single line comments

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let's see a simple example of PHP multiple line comment.
1.            <?php  
2.            /* 
3.            Anything placed 
4.            within comment 
5.            will not be displayed 
6.            on the browser; 
7.            */  
8.            echo "Welcome to PHP multi line comment";  
9.            ?> 

Output:

Welcome to PHP multi line comment

No comments:

Post a Comment