By default, displaying errors is turned off because you don't want a "customer" seeing the error messages.
Check this page in the PHP documentation for information on the 2 directives: error_reporting and display_errors. display_errors is probably the one you want to change.
So you have 3 options:
(1) You can check the error log file as it will have all of the errors (unless logging has been disabled). To enable error logging make sure that log_errors configuration directive is set to On. Logs are also helpful when error is happened not in PHP but emitted by web-server.
(2) You can add the following 2 lines that will help you debug errors that are not syntax errors happened in the same file:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Note that on a live server the latter should be set to Off (but only the latter, because you still need to learn about all errors happened, from the log file).
However, for syntax errors happened in the same file, the above commands won't work and you need to enable them in the php.ini. If you can't modify the php.ini, you may also try to add the following lines to an .htaccess file, though it's seldom supported nowadays:
php_flag display_errors on
php_value error_reporting -1
(3) Another option is to use an editor that checks for errors when you type, such as PhpEd, VSCode or PHPStorm. They all come with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)
Answer from Darryl Hein on Stack OverflowBy default, displaying errors is turned off because you don't want a "customer" seeing the error messages.
Check this page in the PHP documentation for information on the 2 directives: error_reporting and display_errors. display_errors is probably the one you want to change.
So you have 3 options:
(1) You can check the error log file as it will have all of the errors (unless logging has been disabled). To enable error logging make sure that log_errors configuration directive is set to On. Logs are also helpful when error is happened not in PHP but emitted by web-server.
(2) You can add the following 2 lines that will help you debug errors that are not syntax errors happened in the same file:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Note that on a live server the latter should be set to Off (but only the latter, because you still need to learn about all errors happened, from the log file).
However, for syntax errors happened in the same file, the above commands won't work and you need to enable them in the php.ini. If you can't modify the php.ini, you may also try to add the following lines to an .htaccess file, though it's seldom supported nowadays:
php_flag display_errors on
php_value error_reporting -1
(3) Another option is to use an editor that checks for errors when you type, such as PhpEd, VSCode or PHPStorm. They all come with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)
The following enables all errors:
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
Also see the following links
- http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors
- http://php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors
- http://php.net/manual/en/function.error-reporting.php
What does double question mark (??) operator mean in PHP - Stack Overflow
What is PHP and why do I need to learn it?
How do I get PHP errors to display? - Stack Overflow
Formatting PHP code in vscode, using prettier, on save
No one should ever format on save, on projects where you work in a team, it will be an never ending whitespace war. And it will seriously pollute your VCS diff's with unrelated whitespace changes, and potentially create conflicts along the way. Please don't.
The only valid use case of reformatting on save is when it's a project requirement, everyone uses the same tooling, editor, and formatting rules. Under any other scenario, please don't.
More on reddit.comVideos
It's the "null coalescing operator", added in php 7.0. The definition of how it works is:
It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
So it's actually just isset() in a handy operator.
Those two are equivalent1:
bar ?? 'something';
$foo = isset(
bar : 'something';
Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce
In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
And original RFC https://wiki.php.net/rfc/isset_ternary
EDIT: As this answer gets a lot of views, little clarification:
1There is a difference: In case of ??, the first expression is evaluated only once, as opposed to ? :, where the expression is first evaluated in the condition section, then the second time in the "answer" section.
$myVar = $someVar ?? 42;
Is equivalent to :
$myVar = isset($someVar) ? $someVar : 42;
For constants, the behaviour is the same when using a constant that already exists :
define("FOO", "bar");
define("BAR", null);
$MyVar = FOO ?? "42";
$MyVar2 = BAR ?? "42";
echo $MyVar . PHP_EOL; // bar
echo $MyVar2 . PHP_EOL; // 42
However, for constants that don't exist, this is different :
$MyVar3 = IDONTEXIST ?? "42"; // Raises a warning
echo $MyVar3 . PHP_EOL; // IDONTEXIST
Warning: Use of undefined constant IDONTEXIST - assumed 'IDONTEXIST' (this will throw an Error in a future version of PHP)
Php will convert the non-existing constant to a string.
You can use constant("ConstantName") that returns the value of the constant or null if the constant doesn't exist, but it will still raise a warning. You can prepended the function with the error control operator @ to ignore the warning message :
$myVar = @constant("IDONTEXIST") ?? "42"; // No warning displayed anymore
echo $myVar . PHP_EOL; // 42
Right now, I am learning what front-end development is and how to use it, as well as using HTML with JavaScript and CSS. In this stage, I am noticing PHP pop up more and more, especially since I'm using WordPress more often. I understand that it is a scripting language, but I am failing to understand its actual usage and application. So, what is a detailed explanation of it?
DEV environment
This always works for me:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
However, this doesn't make PHP to show parse errors occurred in the same file. Also, these settings can be overridden by PHP. In these cases the only way to show those errors is to modify your php.ini (or php-fpm.conf) with this line:
display_errors = on
(if you don't have access to php.ini, then putting this line in .htaccess might work too):
php_flag display_errors 1
PROD environment
Note that above recommendation is only suitable for the DEV environment. On a live site it must be
display_errors = off
log_errors = on
And then you'll be able to see all errors in the error log. See Where to find PHP error log
AJAX calls
In case of AJAX call, on a DEV server, open DevTools (F12), then Network tab. Then initiate the request which result you want to see, and it will appear in the Network tab. Click on it and then the Response tab. There you will see the exact output.
While on a live server just check the error log all the same.
You can't catch parse errors in the same file where error output is enabled at runtime, because it parses the file before actually executing anything (and since it encounters an error during this, it won't execute anything). You'll need to change the actual server configuration so that display_errors is on and the approriate error_reporting level is used. If you don't have access to php.ini, you may be able to use .htaccess or similar, depending on the server.
This question may provide additional info.