Welcome back! In this installment of “human-speak for developers”, we go over some key questions about the language PHP.

**need to smooth out intro and ending

PHP is a server-side scripting language that allows you to mix logic and HTML. Developed by Rasmus Lerdorf in 1994, Hypertext Preprocessor (aka PHP) is an open source server-side scripting language that is widely used for creation of dynamic web applications. PHP is an interpreted language that is implemented in C. It is also a loosely typed language , meaning it doesn’t need to be told which kind of Datatype a Variable is; depending on the variable’s value, PHP will automatically convert the variable to the correct datatype.

What is T_PAAMAYIM_NEKUDOTAYIM ?

T_PAAMAYIM_NEKUDOTAYIM is scope resolution operator used as :: (double colon) to call static methods/variables of a Class.

What is the default session time?

Default session time in PHP is 1440 seconds (24 minutes) and Default session storage path is temporary folder/tmp on server.

Change the default session time parameters so the server keeps session data for atleast an hour and clients remember session IDs for exactly an hour.

<?php

// server should keep session data for AT LEAST 1 hour

ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour

session_set_cookie_params(3600);

?>

What are PHP Magic Functions/Methods?

Programmer-defined functions starting with __ names that always live in a PHP class. They always defined inside classes, and are not stand-alone (outside of classes) functions. The definition of each magical function is designated by the programmer. Magic functions will never be called directly, they operate ‘behind the scenes’. In a nutshell, Magic Functions allow you simplify syntax in certain circumstances

List the available Magic functions/methods in PHP

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo()

What are the 9 datatypes used in PHP, and what 3 categories do they fall into?

Scalar: Integer, Boolean, Float, and String

Compound: Array, Object, and Callable

Resource: Resource and Null

What is the execution time of a PHP script, and how can it be changed?

The default execution time is 30 seconds. If a php script runs longer than 30 seconds, PHP stops the script and reports an error.

The default time can be altered by changing the max_execution_time directive in your php.ini file, or by calling ini_set(‘max_execution_time’, 300); //300 seconds = 5 minutes function at the top of your php script.

What is the purpose of @ in PHP?

It is used to suppress error messages. When @ is added before any statement, any runtime errors occurring on that line will be handled by PHP.

What are the 13 errors returned in PHP?

E_ERROR: A fatal error that causes script termination.

E_WARNING: Run-time warning that does not cause script termination.

E_PARSE: Compile time parse error.

E_NOTICE: Run time notice caused due to error in code.

E_CORE_ERROR: Fatal errors that occur during PHP initial startup.

(installation)

E_CORE_WARNING: Warnings that occur during PHP initial startup.

E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.

E_USER_ERROR: User-generated error message.

E_USER_WARNING: User-generated warning message.

E_USER_NOTICE: User-generated notice message.

E_STRICT: Run-time notices.

E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error

E_ALL: Catches all errors and warnings.

Is multiple inheritance supported in PHP ?

No, multiple inheritance is not supported.

How can you post JSON data in a URL using curl in PHP?

$url='https://www. codestaff._io/get_details';

$jsonData='{"name":"phpcodestaffdata",

"email":"phpcodestaffdata@codestaff.io"

,'age':36

}';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

curl_close($ch);

How can you add 301 redirects ?

header("HTTP/1.1 301 Moved Permanently");

header("Location: /option-a");

exit();

Is it preferable to store logs in a database or in a file?

Databases are preferable, as they provide more flexibility and reliability than does logging to a file. It is easy to run queries on databases and generate statistics than it is for flat files. Writing to a file has more overhead and will cause your code to block or fail in the event that a file is unavailable. Inconsistencies caused by slow replication in AFS may also pose a problem to errors logged to files. If you have access to MySQL, use a database for logs, and when the database is unreachable, have your script automatically send an e-mail to the site administrator.

What are the differences between GET and POST methods in form submitting, give the case where we can use get and we can use post methods?

The difference between METHOD=”GET” (the default) and METHOD=”POST” is primarily defined in terms of form data encoding. According to the technical HTML specifications, GET means that form data is to be encoded (by a browser) into a URL while POST means that the form data is to appear within the message body of the HTTP request.

Get

Post

History:

Parameters remain in browser history because they are part of the URL

Parameters are not saved in browser history.

Bookmarked:

Can be bookmarked.

Can not be bookmarked.

BACK button/re-submit behavior:

GET requests are re-executed but may not be re-submitted to the server if the HTML is stored in the browser cache.

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.

Hacked:

Easier to hack for script kiddies

More difficult to hack

Restrictions on form data type:

Yes, only ASCII characters allowed.

No restrictions. Binary data is also allowed.

Security:

GET is less secure compared to POST because data sent is part of the URL. So it’s 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.

Restrictions on form data length:

Yes, since form data is in the URL and URL length is restricted. A safe URL length limit is often 2048 characters but varies by browser and web server.

No restrictions

Usability:

GET method should not be used when sending passwords or other sensitive information.

POST method used when sending passwords or other sensitive information.

Visibility:

GET method is visible to everyone (it will be displayed in the browsers address bar) and has limits on the amount of information to send.

POST method variables are not displayed in the URL.

Cached:

Can be cached

Not Cached

Large variable values:

7607 characters maximum size.

8 Mb max size for the POST method.

How can you access the standard error stream in PHP?

$stderr = fwrite("php://stderr");

$stderr = fopen("php://stderr", "w");

$stderr = STDERR;