+ 1

What is the correct way to output html from a php function?

I have a php function with a loop. I want my function to loop through an array and output something along the lines of the following to any of my pages calling it - <div class="style">html from php result 1</div> <div class="style">html from php result 2</div> <div class="style">html from php result 3</div> Currently my loop ends with this: echo "<div class=\"style\">".$result."</div>" This feels very awkward ... especially having to escape the quote marks. I feel like this isn't the right way but I'm struggling to find others. What is the correct way to output html from a php function?

18th Jun 2018, 8:04 PM
Greg Hurlock
Greg Hurlock - avatar
4 Respostas
0
not really what I'm asking. I want to know what best practice amongst web developers is. For example in C# a developer might tell me to use StringBuilder for a lot of strings or String.Format when there's several variables to go in to a single string. Does php have anything better than concatenating html together with dots? I know about join() but it only takes 2 inputs. Would best practice be sticking everything in to an array and using join() ?
18th Jun 2018, 8:40 PM
Greg Hurlock
Greg Hurlock - avatar
0
That's better than what I'm currently doing, definitely. I didn't realise you could do that with printf(). I'm most curious about common practice ... if possible, i'd like to find out from someone who works as a php developer about expectations.
18th Jun 2018, 9:01 PM
Greg Hurlock
Greg Hurlock - avatar
0
I've now done some reading and discovered that in my question ... I'm doing it wrong. echo "<div class='echoslide'>$imageresult</div>"; Tonight I (re?)learnt php echo doesn't require concatenate operators to include variables in a string. I can then get rid of the escape marks by using single quotes. Thanks for showing me the print functions Mohamed. I'll keep them in mind.
18th Jun 2018, 9:27 PM
Greg Hurlock
Greg Hurlock - avatar
0
note to self also read up on file_get_contents();
19th Jun 2018, 2:39 PM
Greg Hurlock
Greg Hurlock - avatar