Help me understand flow control!

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
alex067
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 14, 2017 2:44 pm

Sun Mar 05, 2017 9:57 pm

I am having a hard time understand the flow of control when having a php script containing html code.

My natural behaviour is to read code from top to bottom and expect the flow to work the same, as in; the statements at the top will execute first, and make its way down to the end. However, I have been reading a lot of examples and other scripts containing html code, and they always add the html code at the bottom AFTER the php script. So when I read these, I get absolutely lost on what executes first; the php script or the html code.

For example, in my very simple script here: I send a user a message, not using mail just by using a database, and when the message gets sent, I want to execute a html script that just has a link titled "Go back" which takes the user to the main dashboard.

So my code works as intended, but I have no idea HOW! The html script at the bottom ONLY executes when the message has been successfully sent, so it executes when if($retval) is true.. but how? I just don't get why this html script didn't execute at the very beginning of the script

$sql = "INSERT INTO mail (MID,SUBJECT,MSG,SENDER,RECEIVER,STATUS,UID) VALUES (DEFAULT,'$subject','$sendMsg','$receiveName','$sendusr','$status','$sendUID')";
mysql_select_db($dbname);
$retval = mysql_query($sql, $con);
if($retval)
{
echo "Message succesfully sent!";
}
else
{
echo "Error: " . $sql . "<br>" . $con->error;
}
}
else
{
echo "That user does not exist!";
}
mysql_close($con);
}
else
{
echo "All fields must be entered!";
}

?>
<html>
<body>
<form action = "dash.php" method ="POST">
<input type ="submit" value="Go back">
</form>
</body>
</html>

chorn
php-forum Fan User
php-forum Fan User
Posts: 559
Joined: Fri Apr 01, 2016 2:18 am

Sun Mar 05, 2017 10:41 pm

it is as you said - PHP interprets the file from top to bottom. HTML is never "executed" as it is no programming language (and js is clientside). but your code would raise at least one syntax error.

thinsoldier
New php-forum User
New php-forum User
Posts: 24
Joined: Sat Dec 02, 2017 3:12 pm

Sat Dec 02, 2017 5:23 pm

Are you not using an editor with syntax highlighting?

Are you not using an IDE smart enough to spot broken php code and warn you?

I have added comments pointing out properly paired { and } and incorrect } that don't pair up.

Code: Select all

<?php

$sql = "INSERT INTO mail (MID,SUBJECT,MSG,SENDER,RECEIVER,STATUS,UID) VALUES (DEFAULT,'$subject','$sendMsg','$receiveName','$sendusr','$status','$sendUID')";
mysql_select_db($dbname);

$retval = mysql_query($sql, $con);

if($retval)
{ // A START
	echo "Message succesfully sent!";
} // A END
else
{ // B START
	echo "Error: " . $sql . "<br>" . $con->error;
} // B END

} // ? END ... WTF? It has no corresponding start.
else
{ // D START
	echo "That user does not exist!";
} // D END
	mysql_close($con);
} // ? END ... WTF? It has no corresponding start.
else
{ // F start
	echo "All fields must be entered!";
} // F end

?>
<html>
<body>
	<form action = "dash.php" method ="POST">
		<input type ="submit" value="Go back">
	</form>
</body>
</html>

thinsoldier
New php-forum User
New php-forum User
Posts: 24
Joined: Sat Dec 02, 2017 3:12 pm

Sat Dec 02, 2017 5:38 pm

alex067 wrote:
Sun Mar 05, 2017 9:57 pm
However, I have been reading a lot of examples and other scripts containing html code, and they always add the html code at the bottom AFTER the php script. So when I read these, I get absolutely lost on what executes first; the php script or the html code.
If you look closely most likely what they are doing is lots of logic heavy php code at the top and creating variables.

Then in the html heavy part below they are using the least amount of php possible:

Code: Select all

...
...
... tons of php code to get all the products from the database 
... and create a lot of variables that will be used later in the html.
...
...
... html that uses the variables created above: 
<div>
   <p> <?=$description;?></p>
   <ul>
   <?php foreach( $products as $p ) { ?>
      <li> <?=$p['name']?> <img src="<?=$p['image_url']?>"> <?=$p['price']?> </li>
  <? } ?>
  </ul>
</div>

Code: Select all

outputs:
<div>
   <p>Racecars are fast</p>
   <ul>
      <li>Formula One Sonic Boom Edition <img src="cars/sonic.jpg"> $875,000 </li>
      <li>Nascar Light Speed Left Turn Express <img src="cars/NLSLTE.jpg"> $875,000 </li>
      <li>Donkey Kong Kart <img src="cars/DKK1.jpg"> $125,000 </li>
      <li>Rust Bucket Death Trap <img src="cars/rusty-venture.jpg"> $125,000 </li>
   </ul>
</div>

thinsoldier
New php-forum User
New php-forum User
Posts: 24
Joined: Sat Dec 02, 2017 3:12 pm

Sat Dec 02, 2017 5:43 pm

Your code would produce something like this which is completely invalid HTML.

Code: Select all

Message succesfully sent!<html>
<body>
	<form action = "dash.php" method ="POST">
		<input type ="submit" value="Go back">
	</form>
</body>
</html>
No text should be sent out before the <html> tag.
Your html document should also start with a <!doctype html>.
Any text appearing before the doctype will invalidate it which will cause your CSS to render in "quirks mode" which is the crappy way browsers handled CSS over a decade ago.

You should be creating a $message variable of some kind before the html and then echo that variable within the html.

Are you not checking the source of what is appearing in your browser? Right click > view source.

Are you not checking your browser dev tools? Right click > inspect element.

Did you not do any basic html and css tutorials before starting to learn PHP ?

Post Reply