Remove warning messages in PHP
You really should fix whatever's causing the warning, but you can control visibility of errors with error_reporting()
. To skip warning messages, you could use something like:
error_reporting(E_ERROR | E_PARSE);
Turn off warnings and errors on PHP and MySQL
When you are sure your script is perfectly working, you can get rid of warning and notices like this: Put this line at the beginning of your PHP script:
error_reporting(E_ERROR);
Before that, when working on your script, I would advise you to properly debug your script so that all notice or warning disappear one by one.
So you should first set it as verbose as possible with:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
UPDATE: how to log errors instead of displaying them
As suggested in the comments, the better solution is to log errors into a file so only the PHP developer sees the error messages, not the users.
A possible implementation is via the .htaccess file, useful if you don't have access to the php.ini file (source).
# Suppress PHP errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
# Enable PHP error logging
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log
# Prevent access to PHP error log
<Files PHP_errors.log>
Order allow,deny
Deny from all
Satisfy All
</Files>
How do I turn off PHP Notices?
You can disable notices by setting error reporting level to E_ALL & ~E_NOTICE;
using either error_reporting
ini setting or the error_reporting()
function.
However, notices are annoying (I can partly sympathize) but they serve a purpose. You shouldn't be defining a constant twice, the second time won't work and the constant will remain unchanged!
Disable PHP Warnings for only PART OF the code
Yes, but...
ideally you'd want to fix those errors (or at least handle them in a correct manner) as it just seems like bad design however what you're looking for is known as the error control operator (@
).
<?php
/* Intentional file error */
$my_file = @file('non_existent_file') or die("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.
Take note...
Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
how to disable warning message and instead show the other message in php?
Use @ symbol like this:
$html_url = 'http://api.biblia.com/v1/bible/content/LEB.html?passage=John333&style=fullyFormatted&key=fd37d8';
$str_html = @file_get_contents($html_url);
Related Topics
How to Get JavaScript Function Data into a PHP Variable
Super Fast Getimagesize in PHP
Yii2 Htaccess - How to Hide Frontend/Web and Backend/Web Completely
PHP Header Redirect Not Working
Use PHP to Set Cron Jobs in Windows
PHP Fatal Error Failed Opening Required File
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
Page Redirect After Certain Time PHP
Resize Iframe Height According to Content Height in It
How to Get Page Content Using Curl
PHP Pdo Prepared Statement - MySQL Like Query
How to Get Url of Current Page in PHP
What Is the Default Lifetime of a Session
How Safe Are Pdo Prepared Statements