PHP - Misc

php

What is the difference between include and require?

If he file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

How can we declare an array?

var $arr = array('apple', 'grape', 'lemon');

What is the purpose of the in_array function?

The in_array method is used to check if a value exists in an array.

What is the purpose of the count() function?

The count() function is used to count all elements in an array or something in an object.

How can we set a cookie in PHP?

setcookie("sample","ram", time() + 3600);

What is the difference between explode and split()?

The split function splits string into array by regular expression. Explode splits a string into array by string.

How can we stop the execution of a PHP script?

The exit() function is used to stop the execution of a page.

What is the purpose of the isset() function in PHP?

The isset() function is used to determine if a variable is set and is not NULL.

How can we define a constant?

Using the define() directive like define('MYCONSTANT', 150)

What is the purpose of the ksort function in PHP?

The ksort function is used to sort an array by key in reverse order.

How can we delete a file using PHP?

The unlink() function deletes the given file from the filesystem.

How can we get the value of the current session ID?

The session_id() function returns the session id for the current session.

What is the difference between the unlink() function and the unset() function?

The unlink() function deletes the given file from the file system. The unset() function makes a variable undefined.

How can we find the length of a string?

Use the strlen() function.

How can we find the length of an array?

Use the count() function.

What is PEAR?

PEAR abbreviate for PHP Extension and Application Repository. It is a framework and repository for reusable PHP component

What is the name of the scripting engine that powers PHP?

PHP is powered by the Zend engine.

How can we merge two arrays?

Use the array_merge function.

What are the different functions in sorting an array?

sort(), arsort(), asort(), ksort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort()

How can we send an HTTP header?

header("Location:index.php");

How can we get the properties (size, type, width, height) of an image using PHP image functions?

  1. To know the image type use the exif_imagetype() function
  2. To know the image size use the getimagesize() function
  3. To know the image width use the imagesx() function
  4. To know the image height use the imagesy() function

How can we determine the current date and time?

Use the date() function

What is the difference between echo, print, and printf?

echo is the basic type used to print out a string. It just shows the content of the message written using it. It can have multiple parameters as well. print is a construct. It returns true on successful output and false if there is no output. It can't have multiple parameters. Printf() is a function and not be used as a construct. It allows the string output to be formatted. It is the slowest medium to print the data out.

How can we get the second of the current time using the date() function?

$second = date("s");

How can we store something into the session?

$_SESSION['name'] = 'Something'

How can we get something from the session?

$_SESSION['name']

What is the purpose of the htmlspecialchars() and urlencode()?

I NEED TO COME BACK TO THIS.

How can we check if the value of a given variable is a number?

Use the is_numeric() function.

How can we check to see if a given variable is empty?

Use the empty() function.

How can we escape data before storing it into the database?

Use the addslashes function

How can we automatically escape incoming data?

We have to enable the magic quotes entry in the PHP configuration file.

Is it possible to remove the HTML tags from data?

Use the strip_tags() function.

How can we use PHP filter?

Use the filter_var function:

if (filter_var($int, FILTER_VALIDATE_INT) === 0 || !filter_var($int, FILTER_VALIDATE_INT) === false) {
    echo("Integer is valid");
} else {
    echo("Integer is not valid");
}

filter_var($ip, FILTER_VALIDATE_IP) === false
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
filter_var($ip, FILTER_VALIDATE_IP);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$url = filter_var($url, FILTER_SANITIZE_URL);

Why does PHP community recommend that we omit the closing tag?

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

Why is using register_global considered as insecure?

The directive itself isn't insecure but the misuse of it is.

When on, register_globals will inject your scripts with all sorts of variables, like request variables from HTML forms. This coupled with the fact that PHP doesn't require variable initialization means writing insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this directive by default. When on, people use variables yet really don't know for sure where they come from and can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by users and disabling register_globals changes this.

// define $authorized = true only if user is authenticated
if (authenticated_user()) {
    $authorized = true;
}

// Because we didn't first initialize $authorized as false, this might be
// defined through register_globals, like from GET auth.php?authorized=1
// So, anyone can be seen as authenticated!
if ($authorized) {
    include "/highly/sensitive/data.php";
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License