Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

Cracking PHP Interviews

Questions and Answers

Bootsity
This book is for sale at http://leanpub.com/cracking-php-interviews

This version was published on 2020-02-07

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.

© 2018 - 2020 Bootsity


CONTENTS

Contents

Chapter 0 - About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 1 - PHP Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Chapter 2 - Datatypes and variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Chapter 3 - Flow control and Iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Chapter 4 - Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Chapter 5 - Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Chapter 6 - Requests and responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Chapter 7 - Sessions and Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Chapter 8 - Filesystem management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Chapter 9 - Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Chapter 10 - OOPs Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Chapter 11 - Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Chapter 12 - Security and Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Chapter 13 - PHP and HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Chapter 14 - PHP and SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Chapter 15 - Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Chapter 16 - New features in PHP 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Video Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

Bootsity
Chapter 0 - About this book
This book is result of efforts of authors at Bootsity and many readers like you.
Our vision is to make best book on PHP interviews.
This vision can be achieved only with the active participation of the whole PHP community, readers
and you.
If you come across anything that should be corrected or improved or added, please write to
echo@bootsity.com.
Happy reading and we wish you best of luck for your interview!

Video course on the book

We’ve also a course on Udemy if you are a visual learner. Details for same are added towards the
end of book.
Chapter 1 - PHP Platform
What is PHP?

PHP or Hypertext Pre-processor is a general purpose programming language written in C and used
by developers to create dynamic web applications. PHP Supports both Procedural Programming and
Object Oriented Programming.
PHP files generally have extension .php and PHP code is generally written between <?php ... ?>
tags.
A hello world program is:

1 <?php
2 echo "hello world";
3 ?>

Is PHP case sensitive?

In PHP, variable names are case-sensitive but function names are not case sensitive. If we define
function name in lowercase, but calling them in uppercase it will work. So, PHP can be called as
partially case-sensitive language.

Is PHP weakly typed language?

Yes, because we don’t need to mention the datatype of variables while declaring it. Variables are
automatically type casted when the values are inserted in it.

How do we install PHP?

PHP is a cross-platform language and we do have multiple choices to install PHP on different
operating systems. We can download PHP from official website and install it or we can make use of
popular bundles like XAMPP and WAMP for Windows, LAMP for Linux and MAMP for iOS.

What is Composer?

Composer is an application-level package manager for the PHP applications that provides a standard
system for managing dependencies of different libraries and others. Some of the features of composer
are:

• dependency resolution for PHP packages


Chapter 1 - PHP Platform 3

• keeping all packages updated


• support autoloading out of the box
• hooks to execute pre and post commands

To manage dependencies, composer uses composer.json file, which looks like:

1 {
2 "autoload": {
3 "psr-0": {
4 "": "src/"
5 }
6 },
7 "require": {
8 "php": ">=5.3.2"
9 },
10 "config": {
11 "bin-dir": "bin"
12 }
13 }

What is Packagist?

Packagist is the primary package repository for Composer. This is where developers publish their
packages or libraries that can be used by other developers which are using composer.

How do we execute a PHP script from the command line?

To execute PHP files from command line, we can use any CLI or shell and pass the .php file to the
php executable. An example is below:

1 ~ /path/to/php /path/to/script.php

What is virtual host?

Virtual hosting is a method for hosting multiple domain names (with separate handling of each
name) on a single server (or pool of servers). This allows one server to share its resources, such as
memory and processor cycles, without requiring all services provided to use the same host name.

What are XAMPP & WAMP?

These are installable packages that can be used to setup development environments on windows
machines easily.
WAMP: acronym for Windows Operating System, Apache(Web server), MySQL Database and PHP
Language.
XAMPP: acronym for X (any Operating System), Apache (Web server), MySQL Database, PHP
Language and PERL.

Bootsity
Chapter 1 - PHP Platform 4

What is Apache server?

Apache is a web server used to handle HTTP Request/Response. After we enable PHP module in
Apache, it accepts HTTP requests from the client and passes it to PHP interpreter. PHP interpreter
performs actions according to HTTP Request and sends back the response to Apache. Apache then
wraps it as a HTTP response and sends back to client.

How to check current PHP version and other information about our system?

We can use function php_info(); inside scripts and using command php -v from command line.

What is interpreter?

PHP interpreter executes command from a PHP script line by line and provides the output to the
executer.

Is PHP compiled or interpreted?

Both, PHP is compiled down to an intermediate bytecode that is then interpreted by the runtime
engine.
PHP compiler is responsible for:

• convert code to a bytecode that can be used by runtime engine.


• resolve functions, names and classes names
• creating symbol table

then, PHP Interpretor does:

• Goes through the bytecode line by line and executes it


• Handles runtime exception

What do we mean by a Framework?

A framework is a layered structure indicating what kind of programs can or should be built and how
they would interrelate. Some computer system frameworks also include actual programs, specify
programming interfaces, or offer programming tools for using the frameworks.
Example: Laravel, Zend, CakePHP

What is MVC?

MVC is an application design pattern that separates the application data and business logic (model)
from the presentation (view). MVC stands for Model, View & Controller. The controller mediates
between the models and views.

Bootsity
Chapter 2 - Datatypes and variables
What is a datatype?

A data type is a classification that specifies which type of value a variable has and what type of
mathematical, relational or logical operations can be applied to it without causing an error.

How many datatypes are there?

Built-in datatypes in PHP:


Integer - whole numbers
String - alphanumeric text
Float - decimal point numbers(also called double)
Boolean - represents logical values(TRUE or FALSE)
Array - collection of elements having same datatype
Object - stores data and information on how to process that data
NULL - no value
Resource - stores a reference to functions and resources external to PHP

What are rules for naming a variable?

Rules for naming a variable are following −

• Variable names must begin with a letter or underscore character.


• A variable name can consist of numbers, letters, underscores but you cannot use characters
like + , - , % , ( , ) . & , etc.

How will you define a constant?

To define a constant you have to use define() function and to retrieve the value of a constant, you
have to simply specifying its name. Unlike with variables, you do not need to have a constant with
a $.

1 define("MINSIZE", 50);
2 echo MINSIZE;

What is the purpose of constant() function?

As indicated by the name, this function will return the value of the constant. This is useful when
you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable
or returned by a function.
Chapter 2 - Datatypes and variables 6

1 define("MINSIZE", 50);
2 echo MINSIZE;
3 echo constant("MINSIZE"); // same thing as the previous line

Only scalar data (boolean, integer, float and string) can be contained in constants.

What are the differences between constants and variables?

• There is no need to write a dollar sign ($) before a constant, where as in variable one has to
write a dollar sign.
• Constants cannot be defined by simple assignment, they may only be defined using the
define() function.
• Constants may be defined and accessed anywhere without regard to variable scoping rules.
• Once the constants have been set, may not be redefined or undefined.

What are the different scopes of variables?

Variable scope is known as its boundary within which it can be visible or accessed from code. In
other words, it is the context within which a variable is defined. There are only two scopes available
in PHP namely local and global scopes.

• Local variables (local scope)


• Global variables (special global scope)
• Static variables (local scope)
• Function parameters (local scope)

When a variable is accessed outside its scope it will cause PHP error undefined variable.

What is string?

A string is a data type used to represent text. It is a set of characters that can also contain spaces
and numbers. For example, the word “Bootsity” and the phrase “Bootsity PHP Tutorials” are both
strings.
To declare strings we can write:

1 $string = "bootsity";

What is the difference between single quoted string and double quoted string?

Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables
with their values as well as specially interpreting certain character sequences.

Bootsity
Chapter 2 - Datatypes and variables 7

1 $variable = "name";
2 $stringEx = 'My $variable will not print!\\n';
3 print($stringEx);
4 $stringEx = "My $variable will print!\\n";
5 print($stringEx);

Output:

1 My $variable will not print!


2 My name will print

How can you convert string into array elements?

explode() function breaks a string into an array. Each of the array elements is a substring of string
formed by splitting it on boundaries formed by the string delimiter.
Syntax:

1 explode(separator,string,limit);

How can you convert array into strings?

The implode() function returns a string from the elements of an array.


The implode() function accept its parameters in either order.
The separator parameter of implode() is optional. However, it is recommended to always use two
parameters for backwards compatibility.
Syntax:

1 implode(separator,array)

How can you concatenate two or more strings?

To concatenate two string variables together, use the dot (.) operator.
Example:

1 $string1 = "Hi! i am";


2 $string2 = "50";
3 echo $string1 . " " . $string2;

Output:

Bootsity
Chapter 2 - Datatypes and variables 8

1 Hi! i am 50

Differentiate between echo and print().

echo and print are more or less the same. They are both used to output data to the screen.
The differences are:

• echo has no return value while print has a return value of 1 so it can be used in expressions.
• echo can take multiple parameters (although such usage is rare) while print can take one
argument.
• echo is faster than print.

Explain static variables.

When a function is completed, all of its variables are normally deleted.


However, sometimes you want a local variable to not be deleted then use static keyword.
Example:

1 function Test() {
2 static $x = 0;
3 echo $x;
4 $x++;
5 }
6 Test();
7 Test();
8 Test();

Output:

1 0 1 2

What are PHP magic constants?

PHP provides a large number of predefined constants to any script which it runs known as magic
constants. PHP magic constants start and end with underscore _
Example:
_LINE_ gives the current line number of the file.

_FILE_ denotes the full path and filename of the file. If used inside an include,the name of the
included file is returned. Since PHP 4.0.2, _FILE_ always contains an absolute path whereas in older
versions it contained relative path under some circumstances.

Bootsity
Chapter 2 - Datatypes and variables 9

Why do we need trim() function?

The trim() function removes whitespaces or other predefined characters from either of the sides(beginning
and ending) of a string.

Can you count the number of words in a string?

The str_word_count() function counts the number of words in a string.


Example:

1 echo str_word_count("Hello world!");

Output:

1 2

How to reverse a string?

strrev() reverses a string.

Example:

1 echo strrev("Hello World!");

Output:

1 !dlroW olleH

How to find the position of a specific text in a string?

strpos() returns the position of the first occurrence of a string inside another string (case-sensitive).
Also note that string positions start at 0, and not 1.

1 echo strpos("I love Bootsity, PHP tutorials!","Bootsity");

Output:

1 7

How can you change cases in a string?

The strtoupper() function converts a string to uppercase and strtolower() function converts a
string to uppercase.
Example:

Bootsity
Chapter 2 - Datatypes and variables 10

1 echo strtoupper("Hello WORLD!") . PHP_EOL;


2 echo strtolower("Hello WORLD!");

Output:

1 HELLO WORLD!
2 hello world!

Can you replace a substring?

The built-in function str_replace() replaces some characters in a string (case-sensitive).


Example:

1 echo str_replace("world","Peter","Hello world!");

Here we have replaced the characters “world” in the string “Hello world!” with “Peter”:

Differentiate between str_replace() and str_ireplace().

The str_ireplace() function php is not sensitive rule and will treat “abc”,”ABC” all combination as
a single.
The str_ireplace() will be less faster becuse it need to convert to the same case. But the difference
will be very little event in a large data.
The str_replace() function is a case sensitive which means that it replaces the string that exactly
matches the string exactly.

Differentiate between printf() and print().

printf() outputs a formatted string whereas print() outputs one or more strings.

Example:

1 print "Hello world!";

Output:

1 Hello world!

Example:

Bootsity
Chapter 2 - Datatypes and variables 11

1 $number = 9;
2 $str = "Beijing";
3 printf("There are %u million bicycles in %s.", $number, $str);

Output:

1 There are 9 million bicycles in Beijing.

Differentiate between strstr() & strchr() functions.

Both the functions finds the first occurrence of a string inside another string so there is no difference.
both are alias of each other.

Differentiate between strstr() and stristr().

stristr() and strstr() both finds the first occurrence of a string inside another string where stristr
is case-insensitive but strstr() is case sensitive.

Can you encode a string in PHP?

string urlencode (string $str ) function is used to encode a string in PHP. This function is
convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass
variables to the next page.

Differentiate between strcmp() and strncmp().

Both the functions compare 2 strings(case-sensitive) but strncmp() compares the strings upto N
numbers.
Example:

1 echo strcmp("Hello world!","Hello earth!");

Output:

1 18 (As the strings are not fully same)

Example:

1 echo strncmp("Hello world!","Hello earth!",6);

Output:

Bootsity
Chapter 2 - Datatypes and variables 12

1 0 (As the strings are same upto first 6 characters)

Is it possible to remove the HTML tags from data?

The strip_tags() function strips a string from HTML, XML, and PHP tags.

What is the use of gettype() in PHP?

The gettype() is a predefined PHP function which is used to know the datatype of any variable.

What is heredoc and nowdoc?

heredoc and nowdoc allows strings to be defined in more than one line without string concatenation.
A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct
is ideal for embedding PHP code or other large blocks of text without the need for escaping.
heredoc example:

1 $name = "Bootsity";
2
3 $here_doc = <<<EOT
4 This is $name website
5 for PHP, Laravel and Angular Tutorials
6
7 EOT;
8
9 echo $here_doc;

Output:

1 This is Bootsity website


2 for PHP, Laravel and Angular Tutorials

nowdoc example:

Bootsity
Chapter 2 - Datatypes and variables 13

1 $name = "Bootsity";
2
3 $here_doc = <<<'EOT'
4 This is $name website
5 for PHP, Laravel and Angular Tutorials
6
7 EOT;
8
9 echo $here_doc;

Output:

1 This is $name website


2 for PHP, Laravel and Angular Tutorials

Bootsity
Chapter 3 - Flow control and
Iterations
Explain if-else statement.

The if statement is a way to make decisions based upon the result of a condition.
For example:

1 $result = 70;
2 if ($result >= 57) {
3 echo "Pass";
4 } else {
5 echo "Fail";
6 }

Output:

1 Pass

Explain switch statement with example.

Switch statement works same as if statements. However the difference is that they can check for
multiple values. Also, you can do the same with multiple if..else statements, but this is not always
the best approach.

1 $flower = "rose";
2 switch ($flower) {
3 case "rose" :
4 echo $flower." costs $2.50";
5 break;
6 case "daisy" :
7 echo $flower." costs $1.25";
8 break;
9 case "orchild" :
10 echo $flower." costs $1.50";
11 break;
12 default :
13 echo "There is no such flower in our shop";
14 break;
15 }
Chapter 3 - Flow control and Iterations 15

Output:

1 rose costs $2.50

Differentiate between switch and if-else statement.

• Check the testing expression: An if-then-else statement can test expressions based on ranges
of values or conditions, whereas a switch statement tests expressions based only on a single
integer, enumerated value, or string.
• switch is better for multi way branching: When compiler compiles a switch statement, it will
inspect each of the case constants and create a jump table that it will use for selecting the path
of execution depending on the value of the expression. Therefore, if we need to select among a
large group of values, a switch statement will run much faster than the equivalent logic coded
using a sequence of if-elses. The compiler can do this because it knows that the case constants
are all the same type and simply must be compared for equality with the switch expression,
while in case of if expressions, the compiler has no such knowledge.
• if-else is better for boolean values: if-else conditional branches are great for variable
conditions that result into a boolean, whereas switch statements are great for fixed data values.
• Speed: A switch statement might prove to be faster sometimes when there are more if-else in
if-else ladder.
• Clarity in readability: A switch looks much cleaner when you have to combine cases. if-
else are quite vulnerable to errors too. Missing an else statement can land you up in havoc.
Adding/removing labels is also easier with a switch and makes your code significantly easier
to change and maintain.

What are the different types of operators?

1. Arithmetic operators
2. Assignment operators
3. Logical operators
4. Comparison operators
5. Unary operators

Explain arithmetic operators.

Operator Use
+ to add numbers
- subtract two numbers
* to multiply two numbers
/ to divide one number by another
% to divide two numbers and return the remainder

Bootsity
Chapter 3 - Flow control and Iterations 16

Explain the assignment operators.

$a = 10;

stores the value 10 in the variable $a

Explain the logical operators.

Operator Use
&& It returns true, if both expression1 and expression2 are true.
! It returns true, if expression is false.
|| It returns true, if either expression1 or expression2 or both of them are
true.
Explain the unary operators.

Operator Use
++ Used to increment the value of an operand by 1.
– Used to decrement the value of an operand by 1.

Explain the comparison operators.

Operator Use
== Equals
!= Doesn’t equal
> Is greater than
< Is less than
>= Is greater than or equal to
<= Is less than or equal to
=== Identical (same value and same type)
!== Not Identical

Differentiate between === and == operators in PHP.

The operator == casts between two different types if they are different, while the === operator
performs a typesafe comparison that means it will only return true if both operands have the same
type and the same value.
Examples:

1 1 === 1: true
2 1 == 1: true
3 1 === "1": false // 1 is an integer, "1" is a string
4 1 == "1": true // "1" gets casted to an integer, which is 1
5 "foo" === "foo": true // both operands are strings and have the same value

Bootsity
Chapter 3 - Flow control and Iterations 17

Explain pre and post increment with example.

Operator Use Explanation


Pre-increment ++$a increments $a by one, then returns $a.
Post-increment $a++ returns $a, then increments $a by one.
Pre-decrement –$a decrements $a by one, then returns $a.
Post-decrement $a– returns $a, then decrements $a by one.

What do you mean by operator overloading?

Operator overloading (less commonly known as ad-hoc polymorphism) is a specific case of


polymorphism (part of the OO nature of the language) in which some or all operators like +, = or ==
are treated as polymorphic functions and as such have different behaviors depending on the types of
its arguments. Operator overloading is usually only syntactic sugar. It can easily be emulated using
function calls.

How many loops are available in PHP?

There are several types of loops in PHP.

• while
• do-while
• for
• foreach

Explain while loop with example.

• The while loop evaluates the test expression.


• If the test expression is true (nonzero), codes inside the body of while loop are executed. The
test expression is evaluated again. The process goes on until the test expression is false.
• When the test expression is false, the while loop is terminated.

Example:

1 $x = 1;
2 while($x <= 5) {
3 echo "The number is: $x <br>";
4 $x++;
5 }

Output:

Bootsity
Chapter 3 - Flow control and Iterations 18

1 The number is: 1


2 The number is: 2
3 The number is: 3
4 The number is: 4
5 The number is: 5

Explain do-while loop with example.

The do…while loop will always execute the block of code once, it will then check the condition, and
repeat the loop while the specified condition is true.
Example:

1 $x = 1;
2 do {
3 echo "The number is: $x <br>";
4 $x++;
5 } while ($x <= 5);

In the above example, first a variable $x is set to 1 ($x = 1). Then, the do while loop will write some
output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or
equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5.

Explain for loop with example.

1 for (init counter; test counter; increment counter) {


2 code to be executed;
3 }

loop has 3 arguments.

• init counter: Initialize the loop counter value


• test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it
evaluates to FALSE, the loop ends.
• increment/decrement counter: Increases/decreases the loop counter value.

Example:

1 for ($x = 0; $x <= 10; $x++) {


2 echo "The number is: $x <br>";
3 }

Output:

Bootsity
Chapter 3 - Flow control and Iterations 19

1 The number is: 0


2 The number is: 1
3 The number is: 2
4 The number is: 3
5 The number is: 4
6 The number is: 5
7 The number is: 6
8 The number is: 7
9 The number is: 8
10 The number is: 9
11 The number is: 10

Explain foreach loop with example.

The foreach provides an easy way to iterate over associative arrays. foreach works only on arrays
and objects, and will issue an error when you try to use it on a variable with a different data type
or an uninitialized variable. There are two syntaxes:

1 foreach (array_expression as $value)


2 statement
3
4 foreach (array_expression as $key => $value)
5 statement

The first foreach loops over the array given by array_expression. On each iteration, the value of the
current element is assigned to $value and the internal array pointer is advanced by one (so on the
next iteration, you’ll be looking at the next element).
The second form will additionally assign the current element’s key to the $key variable on each
iteration.

How can you implement an infinite loop in PHP?

This 3 loops can be used to achieve infinite loops in PHP.

1. while
2. do-while
3. for

Examples:

Bootsity
Chapter 3 - Flow control and Iterations 20

1 while (1)
2 //statement`
3
4 for (;;;)
5 //statement`
6
7 do {
8 //statement
9 }
10
11 while (1) {
12 }

In all the above 3 cases, the loops will execute infinite times as the condition is always true i.e., it
returns 1 for while and do-while loops and no ending condition for the for loop.

How can you implement recursion in PHP?

Recursion is the phenomenon of calling a function from within itself.

1 function factorial($n) {
2 // Base case
3 if ($n == 0) {
4 echo "Base case: \$n = 0. Returning 1...<br>";
5 return 1;
6 }
7
8 // Recursion
9 echo "\$n = $n: Computing $n * factorial(".($n-1).")...<br>";
10 $result = ($n * factorial($n-1));
11 echo "Result of $n * factorial(" .($n-1).") = $result. Returning $result...<br>";
12 return $result;
13 }
14
15 echo "The factorial of 5 is: " . factorial(5);

Output:

1 The factorial of 5 is: 120

Bootsity
Chapter 3 - Flow control and Iterations 21

Differentiate between iteration and recursion.

• The primary difference between recursion and iteration is that is a recursion is a process, always
applied to a function. The iteration is applied to the set of instructions which we want to get
repeatedly executed.
• Recursion is usually much slower because all function calls must be stored in a stack to allow
the return back to the caller functions. In many cases, memory has to be allocated and copied
to implement scope isolation.

Explain break statement with example.

When a break statement is encountered inside a loop, the loop is immediately terminated and the
program control resumes at the next statement following the loop. It can be used to terminate a case
in the switch statement.
Example:

1 for ($a = 1; $a < 6; $a++) {


2 echo $a;
3 if ($a > 3) {
4 break;
5 }
6 }

Output:

1 1234

Explain continue statement with example.

The continue statement is used inside loops. When a continue statement is encountered inside a loop,
control jumps to the beginning of the loop for next iteration, skipping the execution of statements
inside the body of loop for the current iteration.

1 for ($a = 1; $a < 6; $a++) {


2 if ($a == 4) {
3 continue;
4 }
5 echo $a;
6 }

Output:

Bootsity
Chapter 3 - Flow control and Iterations 22

1 1235 (As it skipped 4 due to the continue statement)

Give example of declaration in php?

The declare construct is used to set execution directives for a block of code. The syntax of declare is
similar to the syntax of other flow control constructs:

1 declare(ticks=1);
2
3 // A function called on each tick event
4 function tick_handler() {
5 echo "tick_handler() called\n";
6 }
7
8 register_tick_function('tick_handler');
9
10 $a = 1;
11 if ($a > 0) {
12 $a += 2;
13 print($a);
14 }

Output:

1 tick_handler() called tick_handler() called tick_handler() called 3tick_handler() ca\


2 lled tick_handler() called

What is require in PHP?

Require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR
level error. In other words, it will halt the script whereas include only emits a warning (E_WARN-
ING) which allows the script to continue.
Syntax:

1 require('somefile.php');

Bootsity
Chapter 4 - Arrays
What is an array?

An array is a data structure which is a collection of elements having same datatype stored in a
contiguous memory location.
There are 3 types of arrays:
Indexed or Numeric Arrays : An array with a numeric index where values are stored linearly.
Associative Arrays : An array with a string index where instead of linear storage, each value can be
assigned a specific key.
Multidimensional Arrays : An array which contains single or multiple array within it and can be
accessed via multiple indices.
Example:

1 $a = array(); // declaration
2 $cars = array("Volvo", "BMW", "Toyota"); // initialization

How can you print an array in PHP?

1. Using print_r() method:

1 $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
2 print_r ($a);

Output:

1 Array
2 (
3 [a] => apple
4 [b] => banana
5 [c] => Array
6 (
7 [0] => x
8 [1] => y
9 [2] => z
10 )
11 )

2. Using var_dump() method: This method is good at the time of debugging.


Chapter 4 - Arrays 24

1 $a = array(1, 2, array("a", "b", "c"));


2 var_dump($a);

Output:

1 array(3) {
2 [0]=>
3 int(1)
4 [1]=>
5 int(2)
6 [2]=>
7 array(3) {
8 [0]=>
9 string(1) "a"
10 [1]=>
11 string(1) "b"
12 [2]=>
13 string(1) "c"
14 }
15 }

What do we mean by the base address of an array?

The base address of an array is the memory location of the first element present in the array i.e., the
0th index element.

What do we mean by keys and values?

In arrays, keys are used to fetch the values at a certain location.


In associative arrays, we can use named key.
Index arrays use numbers starting from 0 by default.

1 // Method 1:
2 $age['Peter'] = "35"; //Peter, Ben & Joe are keys
3 $age['Ben'] = "37"; //35, 37 & 43 are values
4 $age['Joe'] = "43";
5
6 // Shorthand:
7 $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
8
9 // New in PHP 7:
10 $age = [35,37,43];

Bootsity
Chapter 4 - Arrays 25

How can we convert array into string?

The implode() function returns a string from the elements of an array.


The implode() function accept its parameters in either order. However, for consistency with
explode(), you should use the documented order of arguments.
Example:

1 $arr = array('Hello','World!','Beautiful','Day!');
2 echo implode(" ",$arr);

Output:

1 Hello World! Beautiful Day!

How can we convert a string into an array elements?

The explode() function breaks a string into an array.


Syntax:

1 explode(separator,string,limit)

The “separator” parameter cannot be an empty string.


Example:

1 $str = "Hello world. It's a beautiful day.";


2 print_r (explode(" ",$str));

Output:

1 Array (
2 [0] => Hello
3 [1] => world.
4 [2] => It's
5 [3] => a
6 [4] => beautiful
7 [5] => day.
8 )

How can we concatenate arrays in PHP?

Using the array_merge() method. The array_merge() function merges one or more arrays into one
array.
Example:

Bootsity
Chapter 4 - Arrays 26

1 $a1 = array("red", "green");


2 $a2 = array("blue", "yellow");
3 print_r(array_merge($a1, $a2));

Output:

1 Array (
2 [0] => red
3 [1] => green
4 [2] => blue
5 [3] => yellow
6 );

Which function counts all the values of an array?

array_count_values() function is used to count all the values of an array. PHP array_count_-
values() returns an array that has the values of given array as keys and their frequency in the array
as values.

1 <?php
2 $a=array("Delhi","Pune","Agra","Delhi","Agra");
3 print_r(array_count_values($a));

Output:

1 Array
2 (
3 [Delhi] => 2
4 [Pune] => 1
5 [Agra] => 2
6 )

How can we check if an element exists in an array?

The in_array() function is used to search for the given string in an array. It returns TRUE if the
given string is found in the array, and FALSE otherwise.
Example:

Bootsity
Chapter 4 - Arrays 27

1 <?php
2 $os = array("Mac", "NT", "Irix", "Linux");
3 if (in_array("Irix", $os)) {
4 echo "Got Irix";
5 }

Output:

1 Got Irix

Which function inserts an element to the end of an array?

PHP array_push() function is used to insert one or more elements to the end of an array.
Example:

1 <?php
2 $array1= array('Mathematics','Physics');
3 array_push($array1,'Chemistry','Biology');
4 print_r($array1);

Output:

1 Array ([0] => Mathematics [1] => Physics [2] => Chemistry [3] => Biology)

What is the use of array_chunk() function?

The array_chunk() function is used to split an array into parts or chunks of new arrays.
Example:

1 <?php
2 $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
3 print_r(array_chunk($cars,2));

Output:

Bootsity
Chapter 4 - Arrays 28

1 Array
2 (
3 [0] => Array
4 (
5 [0] => Volvo
6 [1] => BMW
7 )
8 [1] => Array
9 (
10 [0] => Toyota
11 [1] => Honda
12 )
13 [2] => Array
14 (
15 [0] => Mercedes
16 [1] => Opel
17 )
18
19 )

The first parameter specifies an array and the second parameter defines the size of each chunk.

Why do we use extract()?

The extract() function imports variables into the local symbol table from an array.
This function uses array keys as variable names and values as variable values. For each element it
will create a variable in the current symbol table.
This function returns the number of variables extracted on success.
Example:

1 $a = "Original";
2 $my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
3 extract($my_array);
4 echo "\$a = $a; \$b = $b; \$c = $c";

Output:

1 $a = Cat; $b = Dog; $c = Horse

Bootsity
Chapter 5 - Functions
What is a function?

A named section of a program that performs a specific task is called a function. In this sense, a
function is a type of procedure or routine.
A valid function name starts with a letter or underscore, followed by any number of letters, numbers,
or underscores.
Example:

1 // Function to say hello


2 function functionToSayHello() {
3 echo "Hello";
4 }
5
6 // Function to find length of the string
7 echo strlen ("Earth");

What are different types of functions?

Types of functions on basis of definition

1. Built-in functions
2. User defined functions

Types of functions on the basis on parameters

1. Non parameterized function


2. Parameterized function

Example:
Chapter 5 - Functions 30

1 // Declaring user-defined function


2 function thisIsUserDefinedFunc() {
3 echo "Hello! from user defined function"
4 }
5
6 function thisIsAnotherUserDefinedFunc($name) {
7 echo "Hello! $name from user defined function"
8 }
9
10 // Calling user defined function
11 thisIsUserDefinedFunc();
12
13 // Calling Built-in function
14 echo strlen ("Earth");
15
16 // Non parameterized function
17 thisIsUserDefinedFunc();
18
19 // Parameterised function
20 thisIsAnotherUserDefinedFunc("Rohit");
21 ?>

What do we mean by “call by value” of function

When we call a function by value, the value of the parameterised variable is passed to the called
function. Any changes to variable inside the called functions are not reflected to the original
parameterised variable.
Example:

1 function adder($str2) {
2 $str2 .= 'Call By Value';
3 }
4 $str = 'Hello ';
5 adder($str);
6 echo $str;

Output:

1 Hello

Bootsity
Chapter 5 - Functions 31

What do we mean by “call by reference”

In case of call by reference, actual value is modified if it is modified inside the function. In such case,
we need to use & symbol with formal arguments. The & represents reference of the variable.
Example:

1 function adder(&$str2) {
2 $str2 .= 'Call By Reference';
3 }
4 $str = 'This is ';
5 adder($str);
6 echo $str;

Output:

1 This is Call By Reference

What do we mean by actual and formal parameters?

Arguments which are mentioned in the function call is known as the actual arguments. For example:
func1(12, 23);

here 12 and 23 are actual arguments.


Actual arguments can be constant, variables, expressions etc.
Arguments which are mentioned in the definition of the function is called formal arguments. Formal
arguments are very similar to local variables inside the function. Just like local variables, formal
arguments are destroyed when the function ends.

1 function factorial($n) {
2 // write logic here
3 }

Here n is the formal parameters.

Maximum how many arguments are allowed in a function in PHP?

There is no limit but you can use func_get_args(), func_get_arg() and func_num_args() to avoid
writing all the arguments in the function definition.

Explain header().

The header() function sends a raw HTTP header to a client.


Syntax:

Bootsity
Chapter 5 - Functions 32

1 header(string,replace,http_response_code)

Here the string specifies the header string to send.

What do we mean by return type of a function?

The return type is similar to a datatype of a function. The common return types are: int, string, float,
boolean etc. All the functions don’t always need to have a return type.

What is the return type of a function that doesn’t return anything?

void which mean nothing.

Do we need to mention the return type of a function explicitly in PHP?

No need to specify return type upon declaration but needs to use return statement within the body
of the function.

What is function that can be used to build a function that accepts any number
of arguments?

func_num_args() returns the number of arguments passed to the function.


func_get_args(void)
Gets an array of the function’s argument list.

Explain the return statement.

return statement immediately terminates the execution of a function when it is called from within
that function.
If no parameter is supplied NULL is returned.

Can we use multiple return statements in a function?

Yes but not in consecutive lines. We should then use the statements upon different conditions
otherwise it will throw an error.
Example:

Bootsity
Chapter 5 - Functions 33

1 function demo() {
2 if (condition)
3 return expression1;
4 else
5 return expression2;
6 }

What is the use of ini_set()?

PHP allows the user to modify some of its settings mentioned in php.ini using ini_set(). This function
requires two string arguments. First one is the name of the setting to be modified and the second
one is the new value to be assigned to it.
Given line of code will enable the display_error setting for the script if it’s disabled.
ini_set('display_errors', '1');

We need to put the above statement, at the top of the script so that, the setting remains enabled till
the end. Also, the values set via ini_set() are applicable, only to the current script. Thereafter, PHP
will start using the original values from php.ini.

What is the difference between unlink and unset functions?

unlink() function is useful for file system handling. We use this function when we want to delete
the files (physically). Example:

1 $xx = fopen('sample.html', 'a');


2 fwrite($xx, '<h1>Hello !!</h1>');
3 fclose($xx);
4 unlink('sample.html');

unset() function performs variable management. It makes a variable undefined. Or we can say that
unset() changes the value of a given variable to null. Thus, in PHP if a user wants to destroy a
variable, it uses unset(). It can remove a single variable, multiple variables, or an element from an
array. Let’s see a sample code.

1 $val = 200;
2 echo $val; // Output will be 200
3 $val1 = unset($val);
4 echo $val1; // Output will be null

Bootsity
Chapter 5 - Functions 34

1 unset($val); // remove a single variable


2 unset($val1, $val2, $val3); // remove multiple variables

How ereg() function works?

The ereg() function searches a string specified by string for a string specified by pattern, returning
true if the pattern is found, and false otherwise.

How eregi() function works?

eregi() − The eregi() function searches throughout a string specified by pattern for a string specified
by string. The search is not case sensitive.

What is the purpose of getdate() function?

The function getdate() optionally accepts a time stamp and returns an associative array containing
information about the date. If you omit the time stamp, it works with the current time stamp as
returned by time().

What is the purpose of date() function?

The date() function returns a formatted string representing a date. You can exercise an enormous
amount of control over the format that date() returns with a string argument that you must pass to
it.

How will you call member functions of a class?

After creating your objects, you will be able to call member functions related to that object. One
member function will be able to process member variable of related object only. Following example
shows how to set title and prices for the three books by calling member functions.

1 $physics−>setTitle("Physics for High School");


2 $chemistry−>setTitle("Advanced Chemistry");
3 $maths−>setTitle("Algebra");
4 $physics−>setPrice(10);
5 $chemistry−>setPrice(15);
6 $maths−>setPrice(7);

Bootsity
Chapter 6 - Requests and responses
How can we display the correct URL of the current webpage?

1 echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

gives us the entire URL of the current webpage.

How to get the information about the uploaded file in the receiving script?

1 $_FILES[$fieldName]['name'] // The Original file name on the browser system.


2 $_FILES[$fieldName]['type'] // The file type determined by the browser.
3 $_FILES[$fieldName]['size'] // The Number of bytes of the file content.
4 $_FILES[$fieldName]['tmp_name'] // The temporary filename of the file in which the u\
5 ploaded file was stored on the server.
6 $_FILES[$fieldName]['error'] // The error code associated with this file upload.

What do we mean by server?

A server is a computer program that provides a service to another computer programs (and its user).
In the client/server programming model, a server program awaits and fulfills requests from client
programs, which may be running in the same or other computers.

What is a client?

A client is the receiving end of a service or the requestor of a service in a client/server model type
of system. The client is most often located on another system or computer, which can be accessed
via a network. This term was first used for devices that could not run their own programs, and were
connected to remote computers that could via a network.

What do you mean by a response?

Response to an event or to something that is generated according to the request from the client side
to the server.
Chapter 6 - Requests and responses 36

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and
servers.
HTTP works as a request-response protocol between a client and server.
A web browser may be the client, and an application on a computer that hosts a web site may be
the server.
Example: A client (browser) submits an HTTP request to the server; then the server returns a
response to the client. The response contains status information about the request and may also
contain the requested content.

What are PHP superglobals?

Several predefined variables in PHP are “superglobals”, which means that they are always accessible,
regardless of scope - and you can access them from any function, class or file without having to do
anything special.
The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

How will we get information sent via GET method?

The PHP provides $_GET associative array to access all the sent information using GET method.

How will you get information sent via POST method?

The $_POST associative array to access all the sent information using POST method.

What is the purpse $_REQUEST variable?

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The PHP
$_REQUEST variable can be used to get the result from form data sent with both the GET and POST
methods.

Bootsity
Chapter 6 - Requests and responses 37

What is the purpose of $_FILES variable?

This is a global PHP variable. This variable is an associate double dimension array and keeps all the
information related to uploaded file.

What is the purpose of $GLOBALS variable in PHP?

It contains a reference to every variable which is currently available within the global scope of the
script. The keys of this array are the names of the global variables.

What is the purpose of $_SERVER variable in PHP?

$_SERVER − This is an array containing information such as headers, paths, and script locations.
The entries in this array are created by the web server. There is no guarantee that every web server
will provide any of these. See next section for a complete list of all the SERVER variables.

What is the purpose of $_COOKIE variable in PHP?

An associative array of variables passed to the current script via HTTP cookies.

What do you mean by environment variable in PHP?

• PHP environment variables allow your scripts to glean certain types of data dynamically from
the server. …
• You can access these variables using the $_SERVER and $_ENV arrays.

What is the purpose of $_ENV variable in PHP?

$_ENV is a superglobal that contains environment variables. Environment variables are provided
by the shell under which PHP is running, so they may vary according to different shells.

What is the purpose of $_SESSION variable in PHP?

$_SESSION − An associative array containing session variables available to the current script.

How will you redirect a page?

The header() function supplies raw HTTP headers to the browser and can be used to redirect it to
another location. The redirection script should be at the very top of the page to prevent any other
part of the page from loading. The target is specified by the Location: header as the argument to the
header() function. After calling this function the exit() function can be used to halt parsing of rest
of the code.

Bootsity
Chapter 6 - Requests and responses 38

What is the purpose $_PHP_SELF variable?

The default variable $_PHP_SELF is used for the PHP script name and when you click “submit” button
then same PHP script will be called.

How will you get the browser’s details using PHP?

One of the environment variables set by PHP is HTTP_USER_AGENT which identifies the user’s browser
and operating system.

What do you mean by HTTP status codes?

Status codes are issued by a server in response to a client’s request made to the server. The first digit
of the status code specifies one of five standard classes of responses. The message phrases shown
are typical, but any human-readable alternative may be provided.

What are the HTTP client error codes?

The HTTP client error codes start with 4. Example: 404 is used for ‘not found’ status.

What are the informational status codes?

1xx: Informational
It means the request has been received and the process is continuing.

What are the HTTP success codes?

2xx: Success
It means the action was successfully received, understood, and accepted.

How do you get the redirection related information?

3xx: Redirection
It means further action must be taken in order to complete the request.

What are the HTTP client error codes?

4xx: Client Error


It means the client sent an invalid request.

What are the HTTP server error codes?

5xx: Server Error


It means the server failed to fulfill an apparently valid request.

Bootsity
Chapter 6 - Requests and responses 39

What is API?

Application Program Interface is a set of functions and procedures that allow the creation of
applications which access the features or data of an operating system, application, or other service.

What is the use of an API?

Basically, an API specifies how software components should interact. Additionally, APIs are used
when programming graphical user interface (GUI) components. A good API makes it easier to
develop a program by providing all the building blocks. A programmer then puts the blocks together.

What are types of API?

Most often-used types of web service:

1. SOAP.
2. XML-RPC.
3. JSON-RPC.
4. REST.

What is REST API?

REST stands for “REpresentational State Transfer”. It is a concept or architecture for managing
information over the internet. REST concepts are referred to as resources. A representation of a
resource must be stateless. It is usually represented by JSON.
API stands for “Application Programming Interface”. It is a set of rules that allows one piece of
software application to talk to another. Those “rules” can include create, read, update and delete
operations.

Why do we need REST API?

In many applications, REST API is a need because this is the lightest way to create, read, update
or delete information between different applications over the internet or HTTP protocol. This
information is presented to the user in an instant especially if you use JavaScript to render the
data on a webpage.

Where REST API is used?

REST API can be used by any application that can connect to the internet. If data from an application
can be created, read, updated or deleted using another application, it usually means a REST API is
used.

Bootsity
Chapter 6 - Requests and responses 40

What is JSON?

JSON: JavaScript Object Notation.


JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.

Why do we need JSON?

1. For sending data


2. For receiving data

How can you exchange data using JSON?

While exchanging data between a browser and a server, the data can only be text.
JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.
We can also convert any JSON received from the server into JavaScript objects.
This way we can work with the data as JavaScript objects, with no complicated parsing and
translations.

Differentiate between JSON & XML.

1. JSON doesn’t use end tag


2. JSON is shorter
3. JSON is quicker to read and write
4. JSON can use arrays
5. XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript
function

What are the advantages of JSON?

JSON is easier to parse. JSON follows simple steps like:

1. Fetch a JSON string


2. JSON.Parse the JSON string

Using XML:

1. Fetch an XML document


2. Use the XML DOM to loop through the document
3. Extract values and store in variables

Bootsity
Chapter 7 - Sessions and Cookies
What is Session?

A session is a temporary and interactive information interchange between two or more communi-
cating devices, or between a computer and user. Session is stored at the server side.

What is Cookie?

Cookie is a small text file (generally up to 4KB) created by a website that is stored in the user’s
computer either temporarily for that session only or permanently. Cookies provide a way for the
website to recognize you and keep track of your preferences and other functionalities like shopping
cart.

Differentiate between Session & Cookie.

Cookies Sessions
stored in browser as text file. server side
can store limited data can store unlimited data
data is on client side and hence easily data is on server side and is difficult ot
accessible access
How do we start a session?

Using session_start() method at the beginning of the script.

How can we set session variable?

Session variables are set with the PHP global variable $_SESSION
Example:

1 // Set session variables


2 $_SESSION["favcolor"] = "green";
3 $_SESSION["favanimal"] = "cat";
4 echo "Session variables are set.";

How to destroy a session?

Using session_destroy() method at the end of the script.


Chapter 7 - Sessions and Cookies 42

How to remove value from session variable?

session_unset() method

When do we need to set session variables?

When a particular user signs in, adds items to cart - to track the particular user activity.
Session variables must be set after using session_start();

When do we need a session and not a cookie?

• Cookies are stored on the client side, so they can be viewed, edited and deleted by the user. So
be careful to not store and sensitive information on cookies.

Sessions are used when more sensitive information like password or id is being passed. Sessions are
not accessible by users and hence more secure.

• You want to store important information such as the user id more securely on the server where
malicious users cannot temper with them.
• You want to pass values from one page to another.
• You want the alternative to cookies on browsers that do not support cookies.
• You want to store global variables in an efficient and more secure way compared to passing
them in the URL

You are developing an application such as a shopping cart that has to temporary store information
with a capacity larger than 4KB.

When do we need a cookie and not a session?

• Http is a stateless protocol; cookies allow us to track the state of the application using small
files stored on the user’s computer.

The path were the cookies are stored depends on the browser.
Internet Explorer usually stores them in Temporal Internet Files folder.

• Personalizing the user experience – this is achieved by allowing users to select their
preferences.
• The page requested that follow are personalized based on the set preferences in the cookies.
• Tracking the pages visited by a user
• Also, cookies last longer than that of a session.

Bootsity
Chapter 7 - Sessions and Cookies 43

How can we set a cookie?

Using
setcookie(name, value, expire, path, domain, secure, httponly);

How to modify a cookie value?

To modify a cookie, just set (again) the cookie using the setcookie() function.

How will we make a check that a cookie is set or not?

You can use isset() function to check if a cookie is set or not.

How to retrieve all cookie values?

1 print_r($_COOKIE);

Output:

1 Array ([user] => abhi [age] => 25 [profile] => developer)

How to delete a cookie?

When deleting a cookie you should assure that the expiration date is in the past.

1 // set the expiration date to one hour ago


2 setcookie("user", "abhi", time()-60*60);

How can we implement ‘remember me’ using PHP?

As the name indicate the meaning that cookies are the method to store the information of a web
page in a remote browser,those information can be retrieved form the browser itself,when the
same user comes back to that page.
The browser stores the message in a small text file that the server embeds on the user’s system.You
can set cookies using the setcookie()function.
PHP transparently supports HTTP cookies,so setcookie() must be called before any
output is sent to the browser.

Classify cookies.

There are 2 types of cookies:

1. Session Based which expire at the end of the session.


2. Persistent cookies which are written on harddisk.

Bootsity
Chapter 7 - Sessions and Cookies 44

How will you delete a cookie?

To delete a cookie you should call setcookie() with the name argument only.

How to track login and logout using PHP?

Session variable must be set when a user logs in and session needs to be destroyed upon logout.

Bootsity
Chapter 8 - Filesystem management
How to create a file?

touch() function is used to create a file.

Example:

1 // create text file


2 touch("data.txt");

What are the other way to write in a file?

An alternative way to write data to a file is to create a file pointer with fopen(), and then write data
to the pointer using PHP’s fwrite() function.
Example:

1 // open and lock file


2 $fo=fopen("output.txt","w");
3 flock($fo,LOCK_EX) or die('ERROR:cannot lock file');
4
5 // write string to file
6 $data="A fish out of water";
7 fwrite($fo, $data);
8
9 // unlock and close file
10 flock($fo,LOCK_UN) or die('ERROR:cannot unlock file');
11 fclose($fo);
12 echo "Data written to file";

How will you check if a file exists or not using php?

File’s existence can be confirmed using file_exist() function which takes file name as an argument.

How to delete a file?

unlink( ) function is used to delete a file.


Example:
Chapter 8 - Filesystem management 46

1 // delete text file


2 unlink("data.txt");

How to copy a file?

Example:

1 // copy text file


2 copy("data.txt","update data.txt");

How to rename file?

rename() function is used to rename file.

Example:

1 // rename text file


2 rename("data.txt","update data.txt");

How to check whether a file or directory exists?

file_exists() function is used to check file or directory existence.

Example:

1 // check file existence


2 echo file_exists("Update resume.doc");

Output:

1 1

How to check path of the file in PHP?

realpath() function is used to check real path of the file.

Example:

1 // check real path of the file


2 echo realpath("Update resume.doc");

How to check size of the file in PHP?

The files length can be found using the filesize() function which takes the file name as its
argument and returns the size of the file expressed in bytes.
Example:

Bootsity
Chapter 8 - Filesystem management 47

1 // check file size


2 echo filesize("notes.txt")." Bytes";

Output:

1 190 Bytes

How to write the contents inside file?

file_put_contents() accepts a filename and path, together with the data to be written to the file,
and then writes the latter to the former
Example:

1 // write string to file


2 $data = "A fish out of water";
3 file_put_contents("output.txt", $data) or die('ERROR: Can not write file');
4 echo "data written inside this file";

Explain file() method.

A way of reading data from a file is file() function, which accepts the name and path to a file and
reads the entire file into an array,
with each element of the array representing one line of the file.
Here’s an example which reads a file into an array and then displays it using foreach loop.
Example:

1 // read file into array


2 $arr = file('output.txt') or die('ERROR: cannot file file');
3 foreach ($arr as $line) {
4 echo $line;
5 }

How To change the file permissions?

Permissions in PHP are very similar to UNIX. Each file has following three types of permissions.

• Read
• Write and
• Execute.

PHP uses the chmod() function to change the permissions of a specific file. It returns TRUE on success
and FALSE on failure.
Following is the Syntax:

Bootsity
Chapter 8 - Filesystem management 48

1 chmod(file,mode)

What are different ways to get the extension of a file?

There are following two ways to retrieve the file extension.

1 $filename = $_FILES['image']['name'];
2 $ext = pathinfo($filename, PATHINFO_EXTENSION);`

How to create a directory using PHP?

Example:

1 mkdir("mydocs");

In this program we use mkdir()function . Pass the directory name inside this function to create the
directory.

How to get files(contents) from directory?

1 $files = scandir("mydocs");
2 print_r($files);

in the above example we get the contents of a directory. use scandir() function , directory name
declare inside this.
scandir() function returns the files in array so stored the return value in a variable( $files).
Now print this using print_r($files) function i.e specially used to print the value and index of array.
it gives an output of an array type with index and their corresponding value.

How to open a directory?

1 $od = openddir("mydocs");

In the above example if we open the directory use opendir() function with directory name
(“mydocs”).
store in variable $files because these open directory variable is going to used in further communi-
cation(for reading the contents).

Bootsity
Chapter 8 - Filesystem management 49

What is include in php?

Files are included based on the file path given or, if none is given, the include_path specified. If the
file isn’t found in the include_path, include will finally check in the calling script’s own directory
and the current working directory before failing. The include construct will emit a warning if it
cannot find a file; this is different behavior from require, which will emit a fatal error.
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on
Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path
will be ignored altogether. For example, if a filename begins with ../, the parser will look in the
parent directory to find the requested file.

What is require_once in php?

The require_once statement is identical to require except PHP will check if the file has already been
included, and if so, not include (require) it again.
“require_once” and “require” are language constructs and not functions. Therefore they should be
written without “()” brackets!

What is include_once in php?

The include_once statement includes and evaluates the specified file during the execution of the
script. This is a behavior similar to the include statement, with the only difference being that if the
code from a file has already been included, it will not be included again, and “include_once” returns
TRUE. As the name suggests, the file will be included just once.
include_once may be used in cases where the same file might be included and evaluated more than
once during a particular execution of a script, so in this case it may help avoid problems such as
function redefinitions, variable value reassignments, etc.

What is require() in PHP?

require() statement takes all the text/code/markup that exists in the specified file and copies it into
the file that uses the include statement.
require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

What is difference between require and include?

The require() function is identical to include(), except that it handles errors differently. If an error
occurs, the include() function generates a warning, but the script will continue execution. The
require() generates a fatal error, and the script will stop.

Bootsity
Chapter 9 - Regular Expressions
What is RegEx?

RegEx or Regular Expressions is a way to express how a computer program should look for a
specified pattern in text and then what the program is to do when each pattern match is found.
For example, a regular expression could tell a program to search for all text lines that contain the
word “Windows 95” and then to print out each line in which a match is found or substitute another
text sequence (for example, just “Windows”) where any match occurs.

Why do we need RegEx?

• Regular expressions simplify identifying patterns in string data by calling a single function.
This saves us coding time.
• When validating user input such as email address, domain names, telephone numbers, IP
addresses,

Highlighting keywords in search results

• When creating a custom HTML template. Regular expressions can be used to identify the
template tags and replace them with actual data.

How preg_match() function works?

The preg_match() function searches string for pattern, returning true if pattern exists, and false
otherwise.

1 $my_url = "www.bootsity.com";
2 echo preg_match("/boot/", $my_url); // prints true

Regualar Expression Notations.


Chapter 9 - Regular Expressions 51

Expression Description
p+ matches any string containing at least one p.
p* matches any string containing zero or more p’s.
p? matches any string containing zero or one p’s.
p matches any string containing a sequence of N p’s
p matches any string containing a sequence of two or three p’s.
p matches any string containing a sequence of at least two p’s.
p$ matches any string with p at the end of it.
^p matches any string with p at the beginning of it.

Regualar Expression Examples.

Expression Description
[^a-zA-Z] matches any string not containing any of the characters ranging from
a through z and A through Z.
p.p matches any string containing p, followed by any character, in turn
followed by another p.
^.$ matches any string containing exactly two characters.
<b>(.*)</b> It matches any string enclosed within <b> and </b>.
p(hp)* matches any string containing a p followed by zero or more instances
of the sequence php.

Bootsity
Chapter 10 - OOPs Concepts
What is OOPs?

Object Oriented Programming(OOPs) is a programming approach based upon objects and data. In
OOP approach, we create templates known as classes and then create their runtime instances known
as objects.

What is an object?

Object is a real life entity which has an identity and behaviour. Each object is an instance of a
particular class or subclass with the class’s own methods or procedures and data variables.

How can we create object of a class?

Once we define our class, we can create as many objects as we like of that class type. Following is
an example of how to create object using new operator.

1 $physics = new Books();


2 $maths = new Books();
3 $chemistry = new Books();

What is a class?

A class is a template that is used to create objects, and to define object data types and methods.
Core properties include the data types and methods that may be used by the object. All class objects
should have the basic class properties.

What are the basic features of OOPs?

1. Abstraction
2. Encapsulation
3. Polymorphism
4. Inheritance

Is PHP purely an object oriented language?

No, PHP is not purely object oriented. PHP supports object oriented approach as well as procedural
approach.

Differentiate between OOPs & POPs.


Chapter 10 - OOPs Concepts 53

OOPs POPs
Takes bottom-up approach. Follows top-down approach
Program is divided into objects Program is divided into functions
Each objects control it’s own data Here, data is global
Data hiding and inheritance are available No such features are there
Java, C++ Pascal, Fortran

What is generalization?

Generalization is the process of extracting shared characteristics from two or more classes, and
combining them into a generalized superclass. Shared characteristics can be attributes, associations,
or methods.

What is specialization?

Specialization is the reverse process of Generalization means creating new sub classes from an
existing class.

What is aggregation?

Aggregation is a relationship between two classes that is best described as a “has-a” and “whole/part”
relationship. It is a more specialized version of the association relationship. The aggregate class
contains a reference to another class and is said to have ownership of that class.

What is composition?

Composition is a special case of aggregation. In other words, a restricted aggregation is called


composition. When an object contains the other object and the contained object cannot exist without
the other object, then it is called composition.

What is association?

The association relationship indicates that a class knows about, and holds a reference to, another
class. Associations can be described as a “has-a” relationship.

What is abstraction?

An abstraction denotes the essential characteristics of an object that distinguish it from all other
kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective
of the viewer.

What is encapsulation?

Encapsulation is the process of binding both attributes and methods together within a class. Through
encapsulation, the internal details of a class can be hidden from outside. The class has methods that
provide user interfaces by which the services provided by the class may be used.

Bootsity
Chapter 10 - OOPs Concepts 54

What is inheritance?

Inheritance is a mechanism wherein a new class is derived from an existing class where a class
inherits or acquires the properties and methods of other classes.

What is super class?

The class from which it’s derived is called the superclass.

What is a sub class?

The derived class (the class that is derived from another class) is called a subclass.

How can you inherit a class in PHP?

We declare a new class with additional keyword extends.

What is a constructor?

If a class name and function name will be similar in that case function is known as constructor.
Constructor is special type of method because its name is similar to class name.
Constructor automatically calls when object will be initializing.

Explain __construct().

By using this function we can define a constructor.


It is known as predefined constructor. Its better than user defined constructor because if we change
class name then user defined constructor treated as normal method.
If predefined constructor and user defined constructor, both are defined in the same class, then
predefined constructor will be treated as a Constructor while user defined constructor treated as
normal method.

Classify constructor.

1. Default constructor
2. Parameterized constructor
3. Copy constructor

What is a destructor?

The Destructor method will be called as soon as there are no other references to a particular object,
or in any order during the shutdown sequence.
__destruct() is used to define destructor.

Bootsity
Chapter 10 - OOPs Concepts 55

Explain $this.

To access or change a class method or property within the class itself, it’s necessary to prefix the
corresponding method or property name with $this which refers to this class.

Explain multiple inheritance.

Multiple inheritance is a feature of some object-oriented computer programming languages in which


an object or class can inherit characteristics and features from more than one parent object or parent
class.

Does PHP support multiple inheritance?

No. PHP only supports multi-level inheritance.

Explain multi-level inheritance.

When a class is derived from a class which is also derived from another class, i.e. a class having more
than one parent classes, such inheritance is called Multilevel Inheritance. The level of inheritance
can be extended to any number of level depending upon the relation.

What is polymorphism?

The Greek word Poly means “many” and morph means property which help us to assign more than
one property with a single name.

1. Method overloading
2. Method overriding

These are the example of polymorphism.

What is method overloading?

Method overloading is the phenomenon of using same method name with different signature.

Does PHP support method overloading?

PHP doesn’t support method overloading concept.

What is method overriding?

Method definitions in child classes override definitions with the same name in parent classes. In a
child class, we can modify the definition of a function inherited from parent class.

Bootsity
Chapter 10 - OOPs Concepts 56

What are interfaces in PHP?

Interfaces are defined to provide a common function names to the implementors. Different imple-
mentors can implement those interfaces according to their requirements. You can say, interfaces are
skeltons which are implemented by developers.

What does the presence of operator ‘::’ represent?

The scope resolution operator :: allows any declaration/definition of methods, variables outside the
body of a class.

How to define a class in PHP?

Class always start with class keyword. After this write class name without parentheses.

1 class demo{
2 function add() {
3 $x = 800;
4 $y = 200;
5 $sum = $x + $y;
6 echo "sum of given no= ". $sum . "<br/>";
7 }
8 function sub() {
9 $x = 1000;
10 $y = 200;
11 $sub = $x - $y;
12 echo "Sub of given no = " . $sub;
13 }
14 }

How will you add a constructor function to a PHP class?

PHP provides a special function called __construct() to define a constructor. You can pass as many
as arguments you like into the constructor function.

How will you add a destructor function to a PHP class?

Like a constructor function you can define a destructor function using function __destruct(). You
can release all the resourceses with-in a destructor.

How will you access the reference to same object within the object in PHP?

The variable $this is a special variable and it refers to the same object ie. itself.

Bootsity
Chapter 10 - OOPs Concepts 57

What do you mean by access modifier?

Access Modifier allows you to alter the visibility of any class member(properties and method).
In php, there are three scopes for class members.

1. Public
2. Protected
3. Private

Explain access modifiers in PHP.

1. Public access modifier is open to use and access inside the class definition as well as outside
the class definition.
2. Protected is only accessible within the class in which it is defined and its parent or inherited
classes.
3. Private is only accessible within the class that defines it.( it can’t be access outside the class
means in inherited class).

Explain final class in PHP.

The final keyword prevents child classes from overriding a method by prefixing the definition with
final.
It means if we define a method with final then it prevent us to override the method.

Explain abstract class.

There must be a abstract keyword that must be return before this class for it to be abstract class to
implement Abstraction.
This class cannot be instantiated. only the class that implement the methods of abstract class can be
instantiated.
There can be more than one methods that can left undefined.

What is interface?

The class that is fully abstract is called interface.


Any class that implement this interface must use implements keyword and all the methods that are
declared in the class must be defined here. otherwise this class also need to be defined as abstract.

Bootsity
Chapter 11 - Exception Handling
What do you mean by an exception?

An exception is a problem that arised during the execution of a program. When an Exception occurs
the normal flow of the program is disrupted and the program or application terminates abnormally.

Define Exception class Hierarchy.

Throwable
– Error
– Arithmetic Error
– Parse Error
– Exception
– Logic Exception
– Runtime Exception

How do we handle exceptions?

When an exception is thrown, code following the statement will not be executed, and PHP will
attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will
be issued with an “Uncaught Exception”.
An exception can be thrown, and caught within PHP.
To handle exceptions, code may be surrounded in a try block.
Each try must have at least one corresponding catch block. Multiple catch blocks can be used to
catch different classes of exceptions.
Exceptions can be thrown (or re-thrown) within a catch block.
Consider:

1 try {
2 print "this is our try block n";
3 throw new Exception();
4 } catch (Exception $e) {
5 print "something went wrong, caught yah! n";
6 } finally {
7 print "this part is always executed n";
8 }
Chapter 11 - Exception Handling 59

Differentiate between exception and error.

• Recovering from Error is not possible. The only solution to errors is to terminate the execution.
Where as you can recover from Exception by using either try-catch blocks or throwing
exception back to caller.
• You will not be able to handle the Errors using try-catch blocks. Even if you handle them
using try-catch blocks, your application will not recover if they happen. On the other hand,
Exceptions can be handled using try-catch blocks and can make program flow normal if they
happen.
• Exceptions are related to application where as Errors are related to environment in which
application is running.

What do we mean by error log?

By default, PHP sends an error log to the server’s logging system or a file, depending on how the
error_log configuration is set in the php.ini file. By using the error_log() function you can send
error logs to a specified file or a remote destination.

How do we see PHP errors?

Display errors could be turned off in the php.ini or your Apache config file. You can turn it on
in the script: error_reporting(E_ALL); ini_set('display_errors', 1); You should see the same
messages in the PHP error log.

What are the exception class functions?

There are following functions which can be used from Exception class.
getMessage() − message of exception
getCode() − code of exception
getFile() − source filename
getLine() − source line
getTrace() − n array of the backtrace()
getTraceAsString() − formated string of trace

What does the expression Exception::__toString means?

Exception::__toString gives the string representation of the exception.

Bootsity
Chapter 12 - Security and
Cryptography
Why do we need cryptography?

• Cryptography can be used for non-technological reasons like hiding physical messages, or
creating ciphers so that only you and your friends can read your messages, but nowadays it is
used for more vital reasons. It is the basis for Data Encryption.
• Cryptography is used to make sure all of the things that I listed above shouldn’t happen. I say
shouldn’t because nothing is perfect, and people can usually find loops holes or ways around
the rules. Cryptography takes math and uses it to develop algorithms for computer systems to
use to secure data either before data transfer or just for secure data storage.

What do we mean by hash functions?

A hash function is any function that can be used to map data of arbitrary size to data of a fixed size.
The values returned by a hash function are called hash values, hash codes, digests, or simply hashes.

Whart is hash function in PHP?

It generates a hash value (message digest).


Syntax:

1 string hash ( string $algo , string $data [, bool $raw_output = FALSE ] )

Example using hash().

1 echo hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');

Output:

1 ec457d0a974c48d5685a7efa03d137dc8bbde7e3

What is encoding and decoding?

Encoding means converting the message or information into a coded form in order to avoid hacking.
Decoding means converting signals into a different or usable form. The reverse process of encoding
is known as decoding.
Chapter 12 - Security and Cryptography 61

What is SHA1?

In cryptography, SHA-1, Secure Hash Algorithm-1 is a cryptographic hash function which takes
an input and produces a 160-bit hash value known as a message digest – typically rendered as a
hexadecimal number, 40 digits long.
The sha1() function uses the US Secure Hash Algorithm 1.
Example:

1 $str = "Hello";
2 echo sha1($str);

Output:

1 f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0

Can sha1 be decrypted?

We cannot say that it is impossible at all (only in our world with limited resources it is). If you have
a simple SHA1 hash, you could decrypt it if you guess what has been encrypted. But this is of course
not efficient. In reality decrypting a large SHA-1 hash is nearly impossible.

What is sha1_file()?

The sha1_file() function calculates the SHA-1 hash of a file.


This function returns the calculated SHA-1 hash on success, or FALSE on failure.

1 $filename = "test.txt";
2 $sha1file = sha1_file($filename);
3 echo $sha1file;

Output:

1 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

What are the disadvantages of sha1()?

Google announced the first SHA1 collision in 2017.


This requires a lot of computing power and resources. Since this never occurred naturally in real
world under normal conditions we can rule out security risks today but not tomorrow.
SHA-1 for hashing passwords requires less computing power and this improves the performance of
your application. Git still using SHA-1 for internal operations.

Bootsity
Chapter 12 - Security and Cryptography 62

What MD5 means?

MD5 (technically called MD5 Message-Digest Algorithm) is a cryptographic hash function whose
main purpose is to verify that a file has been unaltered.
The md5() function calculates the MD5 hash of a string.
The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

1 $str = "Hello";
2 echo md5($str);

Output:

1 8b1a9953c4611296a827abf8c47804d7

Why can not a MD5 hash be decrypted?

There are no services which allow you to “decrypt MD5” because, MD5 is not an encryption
algorithm. Hash functions take an input and create an output which cannot be turned back into
the input. Because its not encrypted. MD5 is a hashing algorithm.

Is md5 reversible?

Now a days MD5 hashes or any other hashes for that matter are pre computed for all possible strings
and stored for easy access. Though in theory MD5 is not reversible but using such databases you
may find out which text resulted in a particular hash value. f(x) = 1 is irreversible. Hash functions
aren’t irreversible.

Compare sha1() and md5().

MD5 and SHA-1 have a lot in common; SHA-1 was clearly inspired on either MD5 or MD4, or both
(SHA-1 is a patched version of SHA-0, which was published in 1993, while MD5 was described as a
RFC in 1992).
The main structural differences are the following:

• SHA-1 has a larger state: 160 bits vs 128 bits.


• SHA-1 has more rounds: 80 vs 64.
• SHA-1 rounds have an extra bit rotation and the mixing of state words is slightly different
(mostly to account for the fifth word).
• Bitwise combination functions and round constants are different.
• Bit rotation counts in SHA-1 are the same for all rounds, while in MD5 each round has its own
rotation count.
• The message words are pre-processed in SHA-0 and SHA-1. In MD5, each round uses one of
the 16 message words “as is”; in SHA-0, the 16 message words are expanded into 80 derived
words with a sort of word-wise linear feedback shift register. SHA-1 furthermore adds a bit
rotation to these word derivation.

Bootsity
Chapter 12 - Security and Cryptography 63

What is enctype?

The enctype attribute specifies how the form-data should be encoded when submitting it to the
server. The enctype attribute can be used only if method="post"
Following are different types of enctype.

• application/x-www-form-urlencoded is the default value if the enctype attribute is not


specified. This is the correct option for the majority of simple HTML forms.
• multipart/form-data is necessary if your users are required to upload a file through the form.
• text/plain is a valid option, although it sends the data without any encoding at all. It is not
recommended, as its behavior is difficult to predict.

Explain each Mcrypt function supported in PHP.

Mcrypt() supports many functions:

• Mcrypt_cbc()- Encrypts data in Cipher block chain mode.


• Mcrypt_cfb()- Encrypts data cipher feedback (CFB) mode
• Mcrypt_decrypt()- Decrypts data.
• mcrypt_encrypt- Encrypts plaintext with given parameters
• mcrypt_generic- Encrypts data
• mcrypt_get_key_size - Get the key size of the specified cipher
• mdecrypt_generic – Decrypts data

What is cryptography authentication?

In cryptography, a message authentication code (MAC), sometimes known as a tag, is a short piece
of information used to authenticate a message—in other words, to confirm that the message came
from the stated sender (its authenticity) and has not been changed.

Bootsity
Chapter 13 - PHP and HTML
What is HTML?

Hypertext Markup Language (HTML) is the standard markup language for creating web pages and
web applications.

1 <HTML>
2 <HEAD>
3 <TITLE>Your Title Here</TITLE>
4 </HEAD>
5 <BODY BGCOLOR="FFFFFF">
6 contents of page
7 </BODY>
8 </HTML>

Differentiate between PHP and HTML.

• PHP is like the machinery behind a dynamic website whereas HTML is the structure and
backbone of a website.
• HTML isn’t a programming language; it is a markup language that is used to create the
structure of a webpage. PHP however is a full-blown programming language that is used to
create most of the advanced functionality you see on modern webpages.

What are the different methods or HTTP verbs of sending data to the server?

HTTP Verb Use


GET used to request data from a specified resource
POST used to send data to a server to create/update a resource
PUT used to send data to a server to create/update a resource
HEAD almost identical to GET, but without the response body
OPTIONS describes the communication options for the target resource
DELETE deletes the specified resource

What’s the difference between GET and POST methods.

• For GET, parameters remain in browser history because they are part of the URL. POST
parameters are not saved in browser history.
• GET data can be bookmarked. In POST method, data can not be bookmarked.
Chapter 13 - PHP and HTML 65

• GET requests are re-executed but may not be re-submitted to server if the HTML is stored in
the browser cache. In POST method, the browser usually alerts the user that data will need to
be re-submitted.
• Encoding type (enctype attribute) application/x-www-form-urlencoded multipart/form-data
or application/x-www-form-urlencoded Use multipart encoding for binary data.
• Parameters can send but the parameter data is limited to what we can stuff into the request
line (URL). Safest to use less than 2K of parameters, some servers handle up to 64K Can send
parameters, including uploading files, to the server.
• GET data is easier to hack for script kiddies but POST data is more difficult to hack
• For GET, only ASCII characters are allowed. No restrictions for POST. Binary data is also
allowed.

Security GET is less secure compared to POST because data sent 8.GET data is saved in browser
history and server logs in plaintext. POST is a little safer than GET because the parameters are not
stored in browser history or in web server logs.

• Since form data is in the URL(for GET) and URL length is restricted. A safe URL length limit is
often 2048 characters but varies by browser and web server. No restrictions for POST method.
• GET method should not be used when sending passwords or other sensitive information. POST
method used when sending passwords or other sensitive information.
• GET method is visible to everyone (it will be displayed in the browser’s address bar) and has
limits on the amount of information to send. POST method variables are not displayed in the
URL.
• GET can be cached whereas POST is not cached.

How can we send email?

The mail() function is used to send emails in PHP. Inside mail() function you can pass three basic
and one optional parameters.

1. Three Basic Parameters : The email address to send(Receiver email), Subject of mail, Con-
tent/message of the mail.
2. Optional Parameters: additional headers you want to include(headers and sender mail)

Bootsity
Chapter 13 - PHP and HTML 66

1 extract($_POST);
2 if (isset($sendmail)) {
3
4 $subject = "Mail Function in PHP";
5 $from = "info@bootsity.com";
6 $message = $name. " " . $mobile . " " . $query;
7 $headers = "From: ".$from;
8 mail($email, $subject, $message, $headers);
9
10 echo "<h3 align='center'>Mail Sent Successfully</h3>";
11 }

HTML Form:

1 <html>
2 <head>
3 <title>Mail function in php - Bootsity</title>
4 </head>
5 <body>
6 <form method="post">
7 <table align="center" border="1">
8 <Tr>
9 <th>Enter Your name</th>
10 <td><input type="text" name="name"/></td>
11 </tr>
12 <tr>
13 <th>Enter Your mobile</th>
14 <td><input type="text" name="mobile"/></td>
15 </tr>
16
17 <tr>
18 <th>Enter Your email</th>
19 <td><input type="email" name="email"/></td>
20 </tr>
21
22 <tr>
23 <th>Enter Your Query</th>
24 <td><textarea name="query"></textarea></td>
25 </tr>
26
27 <tr>
28 <td align="center" colspan="2">
29 <input type="submit" value="Send Mail" name="sendmail"/>

Bootsity
Chapter 13 - PHP and HTML 67

30
31 </tr>
32 </table>
33 </form>
34 </body>
35 </html>

In above example , a mail is sent to info@gmail.com which is receiver address. Subject, messages
and headers are also given.

How file upload works?

First, we define FORM method as POST and enctype=’multipart/form-data’ both property must be
defined for uploading a file.

1 <html>
2 <body>
3 <form action="upload.php" enctype="multipart/form-data" method="post">
4 Your File Name <input type="file" name="file"/><br/>
5 <input type="submit" value="Upload" name="upload"/>
6 </form>
7 </body>
8 </html>

The enctype attribute of the <form> tag specifies which content-type to use when submitting the
form.
‘multipart/form-data’ is used when a form requires binary data, like the contents of a file, to be
uploaded.
The type=’file’ attribute of the <input> tag specifies that the input should be processed as a file.
For example, when viewed in a browser, there will be a browse-button next to the input field.
Below is the code for upload.php

Bootsity
Chapter 13 - PHP and HTML 68

1 if ($_POST['upload']) {
2 move_uploaded_file(
3 $_FILES["file"]["tmp_name"],
4 "upload/" . $_FILES["file"]["name"]
5 );
6
7 echo "Upload: " . $_FILES["file"]["name"] . "<br>";
8
9 echo "Type: " . $_FILES["file"]["type"] . "<br>";
10
11 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
12
13 echo "Stored in: " . $_FILES["file"]["tmp_name"];
14 }

Bootsity
Chapter 14 - PHP and SQL
What is SQL?

SQL stands for Structured Query Language. SQL is a standardized query language for requesting
information from different databases

Why do we need SQL with PHP?

In web applications, we need to store data. This data may relate to user, his activity, transaction and
others. Modern applications store this data in RDBMS like MySQL or Oracle. To manage the data in
these systems, we need SQL. Consider example of storing some user information in database:

1 $data = "INSERT INTO users(firstname, lastname, email)


2 VALUES ('Maya', 'Sharma', 'maya@bootsity.com')";`

How many types of database connections possible in PHP.

• MySQLi (object-oriented)
• MySQLi (procedural)
• PDO

Adavantages of PDO over MySQLi approach.

1. Object Oriented
2. Bind parameters in statements (security)
3. Allows for prepared statements and rollback functionality (consistency)
4. Throws catcheable exceptions for better error handling (quality)
5. One API for a multiple of RDBMS brands

How connect to the database using PDO?


Chapter 14 - PHP and SQL 70

1 $servername = "localhost";
2 $username = "username";
3 $password = "password";
4
5 try {
6 $conn = new PDO("mysql:host=$servername", $username, $password);
7 // set the PDO error mode to exception
8 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
9 $sql = "CREATE DATABASE myDBPDO";
10 // use exec() because no results are returned
11 $conn->exec($sql);
12 echo "Database created successfully<br>";
13 } catch(PDOException $e) {
14 echo $sql . "<br>" . $e->getMessage();
15 }
16 $conn = null;

What is SQL injection?

SQL injection is a code injection technique that might destroy your database.
It usually occurs when you ask a user for input, like their username/userid, and instead of a name/id,
the user gives you an SQL statement that you will unknowingly run on your database.
Consider:
SELECT * FROM Users WHERE UserId = 105 OR 1=1;

The SQL above is valid and will return ALL rows from the “Users” table, since OR 1=1 is always
TRUE.
A hacker might get access to all the user names and passwords in a database, by simply inserting
105 OR 1=1 into the input field.
Does the example above look dangerous? What if the “Users” table contains names and passwords?

Bootsity
Chapter 15 - Miscellaneous
How can we get the browser’s details using PHP?

One of the environment variables set by PHP is HTTP_USER_AGENT which identifies the user’s browser
and operating system.

What is the use of Xdebug extension?

Xdebug. It uses the DBGp debugging protocol for debugging. It is highly configurable and adaptable
to a variety of situations.
Xdebug provides following details in the debug information:

• Stack and function trace in the error messages.


• Full parameter display for user defined functions.
• Displays function name, file name and line indications where the error occurs.
• Support for member functions.
• Memory allocation

It can also be used for:

• Profiling information for PHP scripts.


• Code coverage analysis.

What is the purpose of php.ini file?

The PHP configuration file, php.ini, is the final and most immediate way to affect PHP’s functional-
ity. The php.ini file is read each time PHP is initialized.in other words, whenever httpd is restarted for
the module version or with each script execution for the CGI version. If your change isn.t showing
up, remember to stop and restart httpd. If it still isn.t showing up, use phpinfo() to check the path to
php.ini.

What is curl?

cURL is a library that lets you make HTTP requests in PHP.


Chapter 15 - Miscellaneous 72

1 $curl_handle=curl_init();
2 curl_setopt($curl_handle, CURLOPT_URL, 'http://www.google.com');
3 curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
4 curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
5 $buffer = curl_exec($curl_handle);
6 curl_close($curl_handle);
7 if (empty($buffer)) {
8 print "Nothing returned from url.<p>";
9 } else {
10 print $buffer;
11 }

Then run it via command line:


php < myphp.php

You ran myphp.php and executed those commands through the php interpreter and dumped a ton
of messy html and javascript to screen.

What is PDO in PHP?

PDO stands for PHP Data Object.


It is a set of PHP extensions that provide a core PDO class and database, specific drivers.
It provides a vendor-neutral, lightweight, data-access abstraction layer. Thus, no matter what
database we use, the function to issue queries and fetch data will be same.
It focuses on data access abstraction rather than database abstraction.

What is autoloading classes in PHP?

With autoloaders, PHP allows the last chance to load the class or interface before it fails with an
error.
The spl_autoload_register() function in PHP can register any number of autoloaders, enable
classes and interfaces to autoload even if they are undefined.

1 spl_autoload_register(function ($classname) {
2 include $classname . '.php';
3 });
4 $object = new Class1();
5 $object2 = new Class2();

In the above example we do not need to include Class1.php and Class2.php. The spl_autoload_-
register() function will automatically load Class1.php and Class2.php.

Bootsity
Chapter 15 - Miscellaneous 73

Discuss die().

While writing your PHP program you should check all possible error condition before going ahead
and take appropriate action when required.

1 if (!file_exists("/tmp/test.txt")) {
2 die("File not found");
3 } else {
4 $file = fopen("/tmp/test.txt", "r");
5 print "Opened file successfully";
6 }

This way you can write an efficient code. Using above technique you can stop your program
whenever it errors out and display more meaningful and user friendly message.

What are variable variable?

Consider:

1 $World = "Foo";
2 $Hello = "World";
3 $a = "Hello";
4
5 $a; //Returns Hello
6 $$a; //Returns World
7 $$$a; //Returns Foo

Bootsity
Chapter 16 - New features in PHP 7
What are return type declarations?

Example:

1 function getTotal(float $a, float $b): float {


2 }

Explain the Exception Hierarchy introduced in PHP 7?

New Hierarchy:

1 |- Exception implements Throwable


2 |- …
3 |- Error implements Throwable
4 |- TypeError extends Error
5 |- ParseError extends Error
6 |- ArithmeticError extends Error
7 |- DivisionByZeroError extends ArithmeticError
8 |- AssertionError extends Error

What is use of Spaceship Operator?

Return 0 if values on either side are equal


Return 1 if value on the left is greater
Return -1 if the value on the right is greater
Example:

1 $compare = 2 <=> 1
2 2 < 1? return -1
3 2 = 1? return 0
4 2 > 1? return 1

What is use of Null Coalesce Operator?

Null coalescing operator returns its first operand if it exists and is not NULL Otherwise it returns
its second operand.
Example:
Chapter 16 - New features in PHP 7 75

1 $name = $firstName ?? $username ?? $placeholder ?? "Guest";

Bootsity
Video Course
We’ve developed a video course, which is available on Udemy.
You will get a special discount too.
http://bit.ly/VIDPHPINT

You might also like