tl;dr Your first sample error—a call to an undefined function—is one you can display by using ini_set()
in the same script. The second error—a syntax error in array(1 2 )
—cannot be displayed using ini_set()
in the same script because PHP never even starts to execute the broken script. To view that error, you have to do one of three things: (1) change the config file, (2) call ini_set()
in another script and include
the broken script, or (3) just look at the server logs instead of displaying the error on-screen.
The Problem with the Script
Your problem isn't with ini_set()
; it's a syntax error with this line:
$test=array(1 2 );
As you know, that is invalid PHP because you just have 2 adjacent numbers, with no comma separator. From the manual (emphasis added):
An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.
You probably meant:
$test=array(1, 2 );
// ^ Notice the comma. This is important!
Why You See No Output
The syntax error causes a parser error, a/k/a the "white screen of death." The parser gives up and dies at the parsing stage. That means it never runs a single line of the script, including your ini_set()
or error_reporting()
calls. So, you don't get any output, though the error would still show up in your server logs.
The critical point: The PHP engine never runs any part of a script with a syntax error. This means ini_set()
and error_reporting()
calls don't run, either. You have to set the configuration option before PHP chokes on the broken script. You can do that in php.ini
or in another file. You cannot do it in the same file that contains the syntax error.
How to Force On-Screen Output of the Error
Based on the comments, it sounds like you are trying to cause a parser error and then display it on the screen. You can't do this with ini_set()
in the same script because PHP fails at the parsing stage. That is, it can't even interpret the script safely, so it stops trying, and no lines of code in that script are ever executed.
To work around this, you need to do one of two things, as explained in this canonical answer:
Add these lines to your php.ini
file:
error_reporting = E_ALL
display_errors = 1
or
wrap the broken script in an include
or require
, like this:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("./broken-script.php");
Nota bene: Never do this in production. If you want to do it in development or QA, fine, but displaying raw PHP errors on-screen in production is a security risk.