Monthly Archives: November 2017

PHP 7 Data Types

Variables can store data of different types, and different data types can do different things. PHP 7 supports 10 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 (aka double)
  4. string

PHP Data Types: Compound Types

Four compound types:

 1. array

2.object

3. callable

4. iterable

PHP Data Types: Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

Note: To check the type and value of an expression, use the var_dump() function or use the gettype() function. To check for a certain type use is_type functions. Some examples:

[php]

<?php
$my_bool = TRUE; // a boolean
$my_str = “Sani”; // a string
$my_str2 = ‘Kamal’; // a string
$my_int = 12; // an integer
$my_double=45.89; // a double
echo var_dump($my_double).”<br>”; // prints out: float(45.89)
echo var_dump($my_int).”<br>”; //prints out:int(12)
echo gettype($my_bool).”<br>”; // prints out: boolean
echo gettype($my_str).”<br>”; // prints out: string
echo gettype($my_double).”<br>”; // prints out:double
// If this is a double, increment it by 3.6
if (is_double($my_double)) {
$my_double+=3.6;
}
// If this is a string, concanate “Hi…”
if (is_string($my_str)) {
$my_str.=” Hi…”;
}
// If $my_bool is a string, print it out
// else print “Something else”
if (is_string($my_bool)) {
echo”String: $my_bool”;
}else{
echo”Something else”;
}
?>

[/php]

 

PHP Operator precedence

image056The precedence of an operator specifies how “tightly” it binds two expressions together. In general, operators have a set precedence, or order, in which they are evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. For example, in the expression 8 + 5 * 2, the answer is 18  and not 26 because the multiplication (“*”) operator has a higher precedence than the addition (“+”) operator. Parentheses may be used to force precedence, if necessary. For instance: (8+2) * 4 evaluates to 40.

Associativity

Operators also have an associativity.
When operators have equal precedence their associativity decides how the operators are grouped.
For example:
- is left-associative, so 5 - 8 - 4 is grouped as (5 - 8) - 4 and evaluates to -7.
= is right-associative, so $x = $y = $z is grouped as $x = ($y = $z).

The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.

N.B:Below table taken from php official documentation. More on php.net

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

Example: Associativity

[php]


<?php
$a = 9 * 3 % 6; // (9 * 3) % 6 = 3
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

$x = 3;
$y = 2;
$x = $y += 3; // $x = ($y += 3) -> $x = 5, $y = 5
?>

[/php]

 

PHP Magic constants

HTML5 article Element

PHP Magic constants

magic.pngPHP provides a large number of predefined constants to any script which it runs. Magic constants have a funny syntax, placing two underscores before and after the constant’s word representation. There are nine magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it’s used on in your script. These special constants are case-insensitive.
A few “magical” PHP constants ate given below:

Name Description
__LINE__ Represents current line number where it is used.
__FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__ The function name.
__CLASS__ The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar).
__METHOD__ The class method name.
__NAMESPACE__ Represents the name of the current namespace.
ClassName::class The fully qualified class name.

Example of magical constants __LINE__ ,__FILE__ and __DIR__

<?php
echo "Line number " . __LINE__ ."
"; // Displays: Line number 2

echo "Line number " . __LINE__ ."
"; // Displays: Line number 3
// Displays the absolute path of this file

echo "The full path of this file is: " . __FILE__;
// Displays the directory of this file

echo "The directory of this file is: " . __DIR__;
?>

Example of magical constants __FUNCTION__, __METHOD__ , __CLASS__ and __NAMESPACE__


namespace MyNamespace;

class PrintFunMethod
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
function getClassName(){

return __CLASS__;

}
function getNamespace(){

return __NAMESPACE__;

}
}
$obj=new PrintFunMethod();
$obj->print_func();
output will be ---- print_func()
$obj->print_method();
output will be ----- trick::print_method
echo $obj->getClassName(); // Displays: PrintFunMethod
echo $obj->getNamespace(); // Displays: MyNamespace

HTML5 article Element

article

The name itself is quite self explanatory, but let’s see how the official documentation describe it:

“A self-contained composition in a document, page, application, or site and that is, in principle, independently distributive or reusable, e.g. in syndication. This could be a forum post, a magazine or newspaper article, a blog entry, a user-submitted comment, an interactive widget or gadget, or any other independent item of content.

You will also find pages with <section> elements containing <section> elements, and <article> elements containing <article> elements.

The following example shows how we structure a blog post content with <article>.

<article>
    <header>
        <h3>
          <a href="#">Lorem ipsum dolor</a>
        </h3>
     </header>
     <section>
         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.
 Non, dolor! Quibusdam accusamus tenetur, adipisci odio dignissimos 
laborum aperiam, commodi necessitatibus consequuntur nemo veritatis!
 Quibusdam, alias. Odio voluptas minima praesentium adipisci!</p>
         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.
 Eveniet aut ducimus magnam eos modi sed culpa quam, architecto hic,
 officia nobis, obcaecati assumenda sint consectetur suscipit dolore
 perspiciatis fugit? Ea!</p>
     </section>
     <footer>
       <small>
     Posted on <time datetime="2017-011-17">November 17</time> 
      in <a href="#">Code</a>
       </small>
     </footer>
</article>

 

 ooo.PNG