Arda
Arda

Reputation: 10929

How to handle errors for debug purposes on PHP? (public strings?)

What I want to do is when an if condition doesn't go as it should, instead of echo'ing the my custom error message in else { }, storing the error message somewhere else and retrieving it from another page.

For example, this is my page with the if condition:

if ($something < 4){

echo 'yes it is less than four';

else { echo 'no it isn\'t less than four';}

I want to for example store these error messages in strings and give them numbers:

if ($something < 4){

$debug11 =  'yes it is less than four';
echo '11';


else { $debug10 = 'no it isn\'t less than four'; echo '10'; }

then let's assume there's a debug.php file with php class that can echo these messages but in order to do so it needs to know what $debug11 is, can it do that without including that php page? is that what public strings are for? or should I just define all of them in debug.php

the point of all this is that jquery will call this file.php and get a message like 11 or 10 which in this case is success or failure then I will be able to know why it failed with debug.php. numbers are easier since I may play with text messages a lot and easier to confirm with numbers than text in if conditions.

Upvotes: 1

Views: 81

Answers (4)

Jamielaststand
Jamielaststand

Reputation: 24

As above I am not 100% sure what you are trying to do, however instead of using variables for your custom error messages it may be better to use Constants. The benefits of them are that the values can't be rewritten unlike your variable where you can change the value within your script.

Your code would look something like this:

define("ERROR1", "It wont Work!");
define("ERROR2", "It still wont Work!");
define("ERROR3", "It must be broken!");

if ($something < 4){
   echo '11';
} else { 
   echo ERROR1; // Prints "It wont Work!"
}

You can store these Constants in your debug.php file and use them on any page you include the file on.

Hope this helps.

Upvotes: 0

Prikkeldraad
Prikkeldraad

Reputation: 1375

  1. Try logging to file: https://www.php.net/manual/en/errorfunc.configuration.php#ini.error-log. You can supply a custom log file in which you can find all your errors.

  2. If you put in a error handler you should be able to create debug messages and store them in another file.

  3. Write own logging mechanism and put log messages in a file.

Upvotes: 1

user680786
user680786

Reputation:

You want to store error-messages and read this messages by another script.
It means you need a storage.
As a storage, you can use files, or memcache, or APC, or queues.
Create logger, which will write messages to the storage, and then in debug.php you will read list of messages from the storage.
I recommend to use Memcache, set_error_handler and trigger_error.

Upvotes: 2

Jay Sidri
Jay Sidri

Reputation: 6406

I'm not sure what you mean by 'public strings', but if you are looking at accessing a variable between 2 pages, you would need to persist them into a session variable at least.

Also you might be better off using PHP assertions to check for error conditions within your code (I think that's what your trying to achieve here):

Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.

Upvotes: 1

Related Questions