Jump to content

Best way to escape PHP code


SaranacLake

Recommended Posts

I haven't coded HTML or PHP in several years and am trying to get back into it.

Seems to me that there were some nifty tricks so that when you were conctenating HTML and PHP you didn't get a birds nest.

Maybe it was using something like { } but I don;t recall.

I also seem to recall that wisely choosing where to use single (') and double (") quote was key!

For example, how could this code be cleaner?

	echo '<table id="membershipPlans">
	<!-- Column Groups -->
	<colgroup>
	  <col id="feature">'."\r";
	  foreach ($plan_names as $p_id => $p_name):
	    echo '<col id="option0'.$p_id.'">'."\r";
	  endforeach;
	  echo '</colgroup>";
	

 

Link to comment
Share on other sites

These things are always subject to personal style. I would do your code something like

<table id="membershipPlans">
	<!-- Column Groups -->
	<colgroup>
		<col id="feature">
<?php foreach ($plan_names as $p_id => $p_name): ?>
		<col id="option0<?=$p_id?>">
<?php endforeach; ?>
	</colgroup>
Link to comment
Share on other sites

Just now, requinix said:

These things are always subject to personal style. I would do your code something like


<table id="membershipPlans">
	<!-- Column Groups -->
	<colgroup>
		<col id="feature">
<?php foreach ($plan_names as $p_id => $p_name): ?>
		<col id="option0<?=$p_id?>">
<?php endforeach; ?>
	</colgroup>

So it looks like you leave the HTML alone and just code the PHP as the exception?  (Whereas the sample I posted tried to encapsulate everything as a PHP echo.

 

What about those magic escape characters I'm trying to remember?

{ } maybe?

I think you use those with variables or arrays or some special place to keep your echo statements clean?

Ringing any bells?

 

Link to comment
Share on other sites

What you're trying to remember is called the complex syntax. Forgot to mention that earlier.

Best practice with PHP is to keep logic (most code) separate from presentation (HTML and such). That means doing as much work in straight PHP as possible and having as little as possible, subject to interpretation, with the display.

With a traditional PHP script where you do all the work for a page in the one file, possibly with includes, you do work in the top "half" of dealing with forms and looking up stuff in the database and all that, and in the bottom "half" you display whatever you need to display to the user. Since the display portion is going to be mostly HTML, it makes more sense to be out of PHP mode for the most part and then briefly jump into PHP mode when you need things like variables or control structures. It's also a lot harder for a good editor or IDE to provide you syntax support for HTML when you have to put them in PHP strings.

Link to comment
Share on other sites

7 minutes ago, requinix said:

What you're trying to remember is called the complex syntax. Forgot to mention that earlier.

Best practice with PHP is to keep logic (most code) separate from presentation (HTML and such). That means doing as much work in straight PHP as possible and having as little as possible, subject to interpretation, with the display.

With a traditional PHP script where you do all the work for a page in the one file, possibly with includes, you do work in the top "half" of dealing with forms and looking up stuff in the database and all that, and in the bottom "half" you display whatever you need to display to the user. Since the display portion is going to be mostly HTML, it makes more sense to be out of PHP mode for the most part and then briefly jump into PHP mode when you need things like variables or control structures. It's also a lot harder for a good editor or IDE to provide you syntax support for HTML when you have to put them in PHP strings.

It sounds like what you are ultimately describing is like Model-View-Controller design, right?

My entire website is built off the one script, two-parts paradigm you mention.

Now, my entire website is not in one script, but for each logical part (e.g. account registration, log-in, upload photo) I have a dedicated PHP sript where the top half looks to see if the form was submitted and any other logic, and then the bottom half is basically a pass-through where i display the HTML and any needed PHP variables as you mention.

Probably my #1 goal for v2.0 is to FINALLY learn how to code "like a real man"  *LOL* and learn how to separate the "presentation" from the "logic", because that is why my website development now moves at a snail's pace - it's so damn hard to manage!!!

This could be a thread unto itself, but since you briught it up, do you have any advice on that topic?

(And should I start a new thread, or can you relabel this one, because i would *love* to get some advice on how to become a better - mor elike "smarter" - PHP developer!!)

 

Link to comment
Share on other sites

6 minutes ago, SaranacLake said:

It sounds like what you are ultimately describing is like Model-View-Controller design, right?

Precisely.

6 minutes ago, SaranacLake said:

Probably my #1 goal for v2.0 is to FINALLY learn how to code "like a real man"  *LOL* and learn how to separate the "presentation" from the "logic", because that is why my website development now moves at a snail's pace - it's so damn hard to manage!!!

It gets easier with practice.

6 minutes ago, SaranacLake said:

This could be a thread unto itself, but since you briught it up, do you have any advice on that topic?

Don't be afraid to do something because you're not sure you're doing it perfectly. I don't mean stuff like security, you really should try to get that right the first time, but site architecture and layout and those things: it's okay if you aim for v1 now and consider redoing some things later.

6 minutes ago, SaranacLake said:

(And should I start a new thread, or can you relabel this one, because i would *love* to get some advice on how to become a better - mor elike "smarter" - PHP developer!!)

If you want to start a new thread, go right ahead. It is a weekend so it's a bit slow around here, but there are enough people around here with experience who could chime in.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.