PHP
What’s new in PHP 7
- Speed Improvement – PHP 7 is a lot more faster than the previous versions of PHP
- Scalar Type Declaration – any calls made to the functions specified must strictly adhere to the specified types
- Return Type Declarations – Adding a return type ensures that only an expected value type returns.
Benefits:
The new implementation of Type Declaration certainly help in making the code easier to read. With PHP 7 you get a versatile type declaration methods which makes your life easier. You can even see at the start of the function, what is required and what will be returned.
Commonly used string functions
strlen()– Return the Length of a Stringstr_word_count()– Count Words in a Stringstrrev()– Reverse a Stringstrpos()– Search For a Text Within a Stringstr_replace()– Replace Text Within a String
strpos() can also be used to check if a string contains a substring by the following code
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}PHP DATE
Characteristics for date:
- d – Represents the day of the month (01 to 31)
- m – Represents a month (01 to 12)
- Y – Represents a year (in four digits)
- l (lowercase ‘L’) – Represents the day of the week
Usage: date(characterstic) : date('Y-m-d');
Require or Include?
requirewill produce a fatal error (E_COMPILE_ERROR) and stop the scriptincludewill only produce a warning (E_WARNING) and the script will continue
PHP SESSION
Sessions are used to save user data throughout the pages, by creating a session via cookie
- Start a session with
session_start(); - Retrieve or set session data via
$_SESSION['xyz'];or$_SESSION['xyz'] = 'value'; - Clear a session by
session_unset(); - Delete session
session_destroy();, deletes cookies as well
Validating data = Determine if the data is in proper form.
Sanitizing data = Remove any illegal character from the data.
Object-Oriented Programming
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- Helps create reusable code
PHP Constructor
A constructor is automatically called when you initiate a class.
function __construct()
PHP Destructor
A desctructor is automatically called when the end of the code is reached.
function __destruct()
PHP – Access Modifiers
public– the property or method can be accessed from everywhere. This is defaultprotected– the property or method can be accessed within the class and by classes derived from that classprivate– the property or method can ONLY be accessed within the class
PHP – Inheritance
When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class.
PHP – Class Constants
Constants cannot be changed once it is declared. Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword.
PHP – Abstract Classes and Methods
Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks.
<?php
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>PHP – Interfaces
Interfaces allow you to specify what methods a class should implement.
Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as “polymorphism”.
Some important learning aspects from today’s day:
- Revised basic PHP functions and methods
- Read about Classes, Inheritance & Interfaces
Need to work more on:
- Interfaces
That’s pretty much how my day today went at rtCamp, looking towards more wonderful days.
Regards
–
Aryan Jasala
WordPress Engineer Trainee
