Why Can't Die Function Be Written before Or After An Echo?

Fellow Programmers,

Read this but irrelevant:

http://php.net/manual/en/function.die.php

  1. Why is it not possible to add the die function to close the mysql connection after making an echo ?
<?php 

//Check for username match in "Usernames" column in "users" table. If there is a match then do the following ...

		$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
		mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $rows);
		if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
			die(
			);
		}

?>

If you add the die function before the echo then it works NOT also. Like so:

<?php 

		//Check for username match in "Usernames" column in "users"	table. If there is a match then do the following ...
		$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
		mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $rows);
		if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			);
			'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'			
		}

?>

However, adding the echo inside the die function works! Why should you add the echo inside the die function when you want to echo a message and only then want the mysql connection to be closed ? Strange!
This works:

<?php 

		//Check for username match in "Usernames" column in "users"	table. If there is a match then do the following ...
		$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
		mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $rows);
		if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
			);
		}

?>

Q2. As you can see from the following code that there is an echo. But the words “echo” are not written and the code works. If you type echo however, then you get an error! Why is that ?

What works …

<?php 

		//Check for username match in "Usernames" column in "users"	table. If there is a match then do the following ...
		$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
		mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $rows);
		if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
			);
		}

?>

What doesn’t work but should have:

<?php 

		//Check for username match in "Usernames" column in "users"	table. If there is a match then do the following ...
		$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
		mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $rows);
		if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			echo 'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
			);
		}

?>

Get error mssg:

Parse error: syntax error, unexpected ‘echo’ (T_ECHO), expecting ‘)’ in C:\xampp\htdocs\sn\reg_short.php on line …

The following doesn’t work either. Note the double quote marks:

<?php 

if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			echo 'That Username ".htmlspecialchars($_POST['member_registration_username'])." is already registered!'
			);
		}

?>

Parse error: syntax error, unexpected ‘echo’ (T_ECHO), expecting ‘)’ in C:\xampp\htdocs\sn\reg_short.php on line

Have you read the php documentation for die/exit?

Read this but irrelevant:

http://php.net/manual/en/function.die.php


$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
	mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
	mysqli_stmt_execute($stmt);
	mysqli_stmt_bind_result($stmt, $rows);
	if (mysqli_stmt_fetch($stmt) && $rows) 
	{
		'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
		die(
		);
	}

I like the way you are surprised when you use a function incorrectly it does not work. Where is your echo in the above example?

If you look at the example on the php site this is how it is supposed to be used:


die('That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
);

I know it is supposed to be like this:


?php >
if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
			);
		}

?>

But my question was why should the echoed mssg be inside the die function ? Why can’t the die function come prior or after the echo ?
Anyway, I got my answer from another source now.
But still, THANK YOU VERY MUCH for your reply and time in checking this issue out.
I might aswell follow you on twitter and facebook, if you don’t mind.

I also have been brought to my sense why the following won’t work:


<?php 

if (mysqli_stmt_fetch($stmt) && $rows) 
		{
			die(
			echo 'That Username ".htmlspecialchars($_POST['member_registration_username'])." is already registered!'
			);
		}

?>

I can’t add a double quote inside a single quote. can’t expect the variable to be translated inside a string.
I’ve been now told the following now from another source:

"die() is a script killer. Any message / html output that you want printed before it DIEs can be put within the () like a normal function call.

That is the way it is supposed to work.

OTherwise you could use the echo function itself - echo ‘Terminated’; but then you’d call die or exit with no parameters.

In short, die is a function that echos text that is sent via a parameter in its function call. It then completely terminates the script."

This thread can be closed/locked now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.