PHP Interview Questions and Answers: Functions, Variables, Sessions, Redis, and More

This article compiles essential PHP interview questions and answers covering output functions, string handling, error reporting, session vs cookie, GET vs POST, code execution results, URL encoding, character set conversion, array operations, Redis benefits, command‑line usage, crontab syntax, request flow, magic methods, and basic variable types.

php Courses
php Courses
php Courses
PHP Interview Questions and Answers: Functions, Variables, Sessions, Redis, and More

1. echo(), print(), print_r() differences echo and print are language constructs for outputting strings (echo can output multiple strings, no return value; print outputs a single string and returns 1), while print_r() is a function that can display complex types such as arrays and objects.

2. Single vs double quotes in strings Single quotes do not parse variables or escape sequences, whereas double quotes can interpolate variables and interpret escape characters.

3. error_reporting purpose The error_reporting() function sets the PHP error reporting level and returns the current level.

4. SESSION vs COOKIE differences Sessions are stored on the server, are more secure, rely on a session ID (often stored in a cookie), and can be persisted in files, databases, or memory; cookies are stored on the client and can be modified.

5. GET vs POST differences GET parameters appear in the URL, are visible, limited in size, and less secure; POST parameters are sent in the request body, allow larger data, and are more secure.

6. Code execution result example

<?php
    $a = 12;
    $b = 012;
    $c = 0x12;
    echo $a,"
",$b,"
",$c;

The output is 12 10 18 because 012 is octal (10 decimal) and 0x12 is hexadecimal (18 decimal).

7. Solving URL Chinese garbled characters Use urlencode() to encode Chinese characters.

8. Converting GB2312 string to UTF‑8 iconv('GB2312','UTF-8','悄悄是别离的笙箫'); 9. Converting a string to an array Examples:

$str = "hello word;From-ajiang"; str_split($str, 3);
explode(";", $str);
preg_split("/-/", $str);

10. String replacement functions

$str = 'linux and php';
str_replace('linux','windows',$str);
preg_replace('/linux|php/','js',$str);

11. String search functions Examples:

preg_match("/php/i","PHP is the web scripting language of choice.");
preg_match_all("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
strpos("I love php, I love php too!","php"); // first occurrence
strrpos("I love php, I love php too!","php"); // last occurrence

12. Benefits of using Redis Fast O(1) operations because data resides in memory, supports rich data types (string, list, set, sorted set, hash), provides transactions, and offers features such as caching, messaging, key expiration, and automatic deletion.

13. Advantages of Redis over Memcached Redis supports richer data structures, is generally faster, and provides data persistence.

14. Running PHP scripts from the command line with arguments Use php -f d:/wamp/test.php arg1 arg2 ... or php -r phpinfo();. The script checks $argc for argument count and accesses values via $argv.

15. Crontab syntax Format: minute hour day month week command. Example to run a backup at 13:00 and 20:00 on weekdays: 0 13,20 * * 1-5 mybackup.

16. What happens when a URL is entered and Enter is pressed The browser resolves the domain to an IP, performs a TCP three‑way handshake, sends the HTTP request, the server processes and responds, and the browser renders the page.

17. Common PHP array functions array_combine(), array_chunk(), array_merge(), array_slice(), array_diff(), array_intersect(), array_search(), array_splice(), array_key_exists(), array_flip(), array_reverse(), array_unique(), range().

18. PHP array sorting functions sort(), rsort(), asort(), ksort(), arsort(), krsort().

19. $_SERVER superglobal examples Shows keys such as HTTP_HOST, SERVER_NAME, SERVER_PORT, SERVER_ADDR, REMOTE_PORT, REMOTE_ADDR, REQUEST_URI, SCRIPT_NAME, QUERY_STRING, SCRIPT_FILENAME.

20. PHP magic methods Includes __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), __autoload(), __debugInfo().

21. Basic PHP variable types Four scalar types: boolean, integer, float (double), string; two compound types: array, object; two special types: resource, NULL.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendVariablesSession
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.