2

I use the 'alert' command in javascript's to check values of variables so as to debug my code. but i cant find any such command in PHP which shows a simple popup box like the alert command. im new to PHP.
is there something i dont know?

Ex:

$username=$_REQUEST["username"];
$password=$_REQUEST["password"];

Here i just want to find out what values are coming in those variables. if it was javascript, i would simply use alert (username) and alert (password). how to do it in php? thank you.

FoolishSeth
  • 3,953
  • 2
  • 19
  • 28
s.mujahid8
  • 189
  • 3
  • 6
  • 15
  • 1
    There are better ways to debug both PHP and JavaScript - like using an actual debugger. – Matt Ball Mar 28 '13 at 04:35
  • I'd recommend you look into firebug and fireftp to debug js and php (both are firefox extensions though they also exist in chrome) you'll find it's much easier to print your variables and call functions there. – Ryoku Mar 28 '13 at 04:36
  • FireBug will not display PHP variables If they are not being printed by PHP in the first place – Hanky Panky Mar 28 '13 at 04:39
  • i use firebug. as said by hanky panky it doesnt help – s.mujahid8 Mar 28 '13 at 04:45
  • Use an IDE such as netbeans (free) or PhpStorm (not free) that has a PHP debugger built in. – vascowhite Mar 28 '13 at 04:51
  • Hi, check this http://stackoverflow.com/questions/13837375/how-to-show-an-alert-box-in-php#answer-13837459 ? – Solace May 04 '15 at 19:32

6 Answers6

6

PHP does not have any function to display a pop-up..

However u can achieve that by writing javascript inside php like this

 echo "<script type='text/javascript'>alert('Username'".$username.");</script>";
 echo "<script type='text/javascript'>alert('Password' ".$password.");</script>";
Sriniwas
  • 505
  • 2
  • 6
  • 21
3

You can use echo, var_dump, print_r or error_log.

Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82
2

Just echo them

echo "Username:".$username;
echo "<br>";
echo "Password:".$password;

You can also use var_dump

PHP is a server side language and does not provide client side functionality like JavaScript alert()

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • 1
    but echo is different. it just prints the value. it doesnt pause the code right? alert command pauses everything and shows the pop up. until we press ok on the popup the remaining code is not executed. this is not the case with 'echo'. – s.mujahid8 Mar 28 '13 at 04:41
  • 1
    That is correct. PHP can not do that for you all by itself. By the time you get to see output HTML from a PHP code, PHP has already completed it work and has gone away (Mostly) – Hanky Panky Mar 28 '13 at 04:42
  • It's pretty common to var_dump() and then exit() when debugging to stop any subsequent redirects, etc. That's not quite a pause but it lets you inspect a point-in-time. – FoolishSeth Mar 28 '13 at 04:48
  • Hi, check this http://stackoverflow.com/questions/13837375/how-to-show-an-alert-box-in-php#answer-13837459 – Solace May 04 '15 at 19:32
1

PHP is server-side, not client-side, so to expect it to behave like JavaScript is somewhat strange.

Check out var_dump instead.

If you really want a JS alert, you could always create a PHP function along these lines:

function debugAlert($var)
{
    echo '<script type="text/javascript">';
    echo 'alert("'.$var.'")'; 
    echo '</script>';
}

Then to output your data, just call debugAlert($username) etc.

That's a very simplistic version - in reality you'd need to escape quotes in the variable value and also include some logic to handle if the variable is an array or object, but it should get you moving in the right direction.

Community
  • 1
  • 1
Mark Parnell
  • 9,175
  • 9
  • 31
  • 36
1

Just use print_r($_REQUEST) . Its displays all the values array('username'=>'','password'=>'');

or echo the value echo $username; and echo $password;

echo "<script type='text/javascript'>alert('username and password'+$username+' & '+$password)";
MKV
  • 913
  • 7
  • 6
0

Use this:

echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123