PHP alert?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

PHP alert?

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP alert?

Post by twinedev »

Nope.
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: PHP alert?

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP alert?

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP alert?

Post 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.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: PHP alert?

Post 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.
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: PHP alert?

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP alert?

Post 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.
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: PHP alert?

Post by wvoyance »

echo mixed with other returning data. Please advice me about PHP's error reporting. What is that? How to use?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP alert?

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP alert?

Post by Celauran »

In my experience, var_dump is generally a more useful debugging tool than echo. You may also want to look into xdebug.
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: PHP alert?

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP alert?

Post 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"
  }
}

*/
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP alert?

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP alert?

Post by social_experiment »

I did not know that ,the casting as a string bit; is there any specific reason for this?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply