Page 1 of 2
PHP alert?
Posted: Thu May 24, 2012 3:21 am
by wvoyance
Has PHP an alert function like javascript so that I can write something on another window?
I would like to check the correctness of my program, if I use echo it will mixed with other return information.
Re: PHP alert?
Posted: Thu May 24, 2012 4:25 am
by twinedev
Nope.
Re: PHP alert?
Posted: Thu May 24, 2012 6:30 am
by wvoyance
so...how can I achieve that purpose to pop up a message in another window?
Or, somehow send a message without interfere with other response?
Re: PHP alert?
Posted: Fri May 25, 2012 12:22 am
by social_experiment
You could embed javascript in the php code which will result in an alert. This is not a good practise but it will serve the purpose you have in mind
Re: PHP alert?
Posted: Fri May 25, 2012 11:32 am
by califdon
Keep in mind that PHP is running on the server, not in the browser. At the time it is running, the browser has not yet received the page, so the best you can do is include something that can run later in the browser (hint: JAVASCRIPT!) when (and if) it is sent. That's what social_experiment told you how to do.
Re: PHP alert?
Posted: Sun May 27, 2012 10:43 pm
by greyhoundcode
Or you could create a function that writes to a log. You could also set up a script that outputs the log, the last 200 entries in reverse chronological order or whatever floats your boat, and keep that open in another tab in your browser. Mix in a little JS magic and it could even refresh itself periodically.
Re: PHP alert?
Posted: Wed May 30, 2012 7:35 am
by wvoyance
califdon wrote:Keep in mind that PHP is running on the server, not in the browser. At the time it is running, the browser has not yet received the page, so the best you can do is include something that can run later in the browser (hint: JAVASCRIPT!) when (and if) it is sent. That's what social_experiment told you how to do.
I understand that. But those reasons do not prevent PHP to pop up a separate page.
Re: PHP alert?
Posted: Wed May 30, 2012 12:08 pm
by califdon
wvoyance wrote:califdon wrote:Keep in mind that PHP is running on the server, not in the browser. At the time it is running, the browser has not yet received the page, so the best you can do is include something that can run later in the browser (hint: JAVASCRIPT!) when (and if) it is sent. That's what social_experiment told you how to do.
I understand that. But those reasons do not prevent PHP to pop up a separate page.
Actually, I believe they do. A web server responds to requests from a browser. How would the server know to what IP address to send new headers? While it is true that PHP can send headers and open a new page, that page must open in a browser and must therefore be requested, directly or indirectly (as in a redirection) by that browser. This is exactly why there is
not the equivalent of an alert() function in PHP.
Edit: I'm afraid I gave you some hasty and incorrect information above. The real issue is that if you are trying to use that sort of technique as a debugging tool, it will not help you if there is a PHP syntax error or anything else that prevents PHP from continuing to parse your script (which is perhaps the most common PHP error), because PHP would simply die and never carry out the action. You can use echo to show the values of variables, and you can use PHP's error reporting to trap and report errors.
Re: PHP alert?
Posted: Thu May 31, 2012 6:49 am
by wvoyance
echo mixed with other returning data. Please advice me about PHP's error reporting. What is that? How to use?
Re: PHP alert?
Posted: Thu May 31, 2012 9:27 am
by social_experiment
http://bg2.php.net/manual/en/function.e ... orting.php
This will report any type of errors related to php, as califdon metioned, syntax errors, notices. the url above will give a more verbose explanation.
Re: PHP alert?
Posted: Thu May 31, 2012 9:30 am
by Celauran
In my experience, var_dump is generally a more useful debugging tool than echo. You may also want to look into xdebug.
Re: PHP alert?
Posted: Wed Jun 13, 2012 6:40 am
by wvoyance
Celauran wrote:In my experience, var_dump is generally a more useful debugging tool than echo. You may also want to look into xdebug.
What is the difference between var_dump and simply echo?
I tried several times, but they seems almost the same.
Re: PHP alert?
Posted: Wed Jun 13, 2012 6:51 am
by social_experiment
var_dump() displays information about the expression passed to it; echo just displays whatever is in a variable at the specific time
Code: Select all
<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
?>
/*
this is displayed in the browser;
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
*/
Re: PHP alert?
Posted: Wed Jun 13, 2012 10:41 am
by Jonah Bron
social_experiment wrote:echo just displays whatever is in a variable at the specific time
... specifically, cast as a string.
http://php.net/manual/en/language.types ... ggling.php
Re: PHP alert?
Posted: Wed Jun 13, 2012 11:56 am
by social_experiment
I did not know that ,the casting as a string bit; is there any specific reason for this?