XML

XML stands for eXtensible Markup Language.

XML was designed to store and transport data.

XML was designed to be both human- and machine-readable.

Data format (“UNIVERSAL”) used for structured document exchange.

XML plays an important role in many different IT systems.

XML is often used for distributing data over the Internet.

It is important (for all types of software developers!) to have a good understanding of XML

Example

<?xml version="1.0" encoding="UTF-8"?>
<site>
 <name>PHPCodez</name>
 <url>phpcodez.com</url>
</site>

Magic Constants

Definition

• PHP Provides a set of predefined constants defined by the php core (EX: E_ERROR; TRUE)

• Several of these can change, depending upon where they are used.

Therefore, not true constants (EX: __DIR__; __NAMESPACE__).

• __DIR__: returns the current working directory
• __FILE__: returns the current working directory and file name
• __FUNCTION__: returns the current function name
• __CLASS__: returns the current class and namespace if defined
• __LINE__: returns the current line number at the point of use
• __METHOD__: returns the current method name
• __TRAIT__: returns the trait name including namespace if defined.
• __NAMESPACE__: returns the current namespace

Example

ClassName::class: returns the fully qualified class name

<?php
 namespace NS {
 class ClassName {}
 echo ClassName::class;
 }
?>

It outputs NS\ClassNam

Constants

Definition

• Identifier for a value that does not change once defined

Naming

• Start with a letter or underscore, are case sensitive, contain only alphanumeric characters and underscores
• By convention use only uppercase letters

Access

• May be defined and accessed anywhere in a program • must be defined before use; cannot be changed subsequently

Variable

Naming

• Start with a “$”
• Can contain letters, numbers, and underscores
• Must start with letter or underscore and by convention should start with a lower case letter or underscore
• Case-sensitive

Referencing
• Variables can be assigned by value or by reference
• The ampersand (&) creates a reference, or alias, and causes both the original variable and alias to point to the same memory value

Initializing

• Variable typing is set automatically by the php parser and called “type juggling/coercion”
• Initializing variables empty is a good practice if it is possible it already points to memory values and you want to start empty
• The function isset() returns a boolean on a passed variable containing a value other than null string, null, or zero

Type Coercion

In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

Type Coercion means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.

<?php
 $phpcodez= 10;   // $phpcodez is an integer
 $bar = (boolean) $phpcodez;   // $phpcodez is a boolean
 ?>

Type Juggling

Type Juggling means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

<?php
 $phpcodez = "1"; // $phpcodez is string (ASCII 49)
 $phpcodez *= 2; // $phpcodez is now an integer (2)
 $phpcodez = $phpcodez * 1.3; // $phpcodez is now a float (2.6)
?>