/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Total newbie – ECHO problem

Hello all,
Just trying to translate a project I recently had into PHP as a learning experience, and everything has worked pretty well so far, but I’m having a problem with one piece. I don’t know if I’ve been looking at this too long and am missing the obvious, or what. Anyway, here is the code:

[CODE]
<?php

session_start();

$sScaleTotal = 90;
$cScaleTotal = 2;
$affScaleTotal = 3;
$acScaleTotal = 4;
$fScaleTotal = 5;
$pScaleTotal = 6;

echo ‘<html><body>’;

echo

<table class=”sectioniiis” cellspacing=”0″>
<tr>
<td class=”sectioniiiheader” colspan=”6″>Section III Norm-Referenced Interpretation</td>
</tr>
<tr>
<td class=”sectioniiiheader”> </td>
<td class=”sectioniiiheader”>Raw score</td>
<td class=”sectioniiiheader”>Standard score</td>
<td class=”sectioniiiheader”>Confidence Interval</td>
<td class=”sectioniiiheader”>Classification</td>
<td class=”sectioniiiheader”>Percentile rank</td>
</tr>
<tr>
<td class=”sectioniiileft”>Social</td>
<td class=”sectioniii”>’ . $sScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
<tr>
<td class=”sectioniiileft”>Competence</td>
<td class=”sectioniii”>’ . $cScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
<tr>
<td class=”sectioniiileft”>Affect</td>
<td class=”sectioniii”>’ . $affScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
<tr>
<td class=”sectioniiileft”>Academic</td>
<td class=”sectioniii”>’ . $acScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
<tr>
<td class=”sectioniiileft”>Family</td>
<td class=”sectioniii”>’ . $fScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
<tr>
<td class=”sectioniiileft”>Physical</td>
<td class=”sectioniii”>’ . $pScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
<tr>
<td class=”sectioniiileft”>Total</td>
<td class=”sectioniii”>’ . $sScaleTotal+$cScaleTotal+$affScaleTotal+$acScaleTotal+$fScaleTotal+$pScaleTotal . ‘</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
</table>

;

echo ‘</body></html>’;

?>
[/CODE]

and here is the html output:

[CODE]
<html><body>20</td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
<td class=”sectioniii”> </td>
</tr>
</table>

</body></html>
[/CODE]

I’ve stripped out the template stuff and the query stuff and anything else not causing the problem. I’ve got it down to just this table. For some reason, it is not echoing anything until the last table row, and it is also not adding the first variable value to the total in that row. Sorry if this is a waste of your time, but like I said, I’m totally new to this and am a little confused. Thanks a lot,

jfherring

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pgreggSep 12.2007 — I believe the problem is this: $sScaleTotal+$cScaleTotal+$affScaleTotal+$acScaleTotal+$fScaleTotal+$pScaleTotal

Both . and + operators have equal precedence which means PHP will parse them left to right... so you'll have string.$sScaleTotal then + $cScaleTotal ,etc which isn't what you want.

Either wrap the math in brackets ($sScaleTotal+$cScaleTotal+$affScaleTotal+$acScaleTotal+$fScaleTotal+$pScaleTotal

) or you could use a , instead of . as an argument separator for echo - that way PHP processes them separately without first having to concatenate all the strings together.

A final alternative is to use heredoc syntax: e.g.

$total = $sScaleTotal+$cScaleTotal+$affScaleTotal+$acScaleTotal+$fScaleTotal+$pScaleTotal

;

echo <<<EOWHATEVER

<table class="sectioniiis" cellspacing="0">

...

<tr>

<td class="sectioniiileft">Social</td>

<td class="sectioniii">$sScaleTotal</td>

<td class="sectioniii"> </td>

<td class="sectioniii"> </td>

<td class="sectioniii"> </td>

<td class="sectioniii"> </td>

</tr>

<tr>

<td class="sectioniiileft">Competence</td>

<td class="sectioniii">$cScaleTotal</td>

<td class="sectioniii"> </td>

...

<td class="sectioniiileft">Total</td>

<td class="sectioniii">$total</td>

<td class="sectioniii"> </td>

...

</tr>

</table>

EOWHATEVER;

Hope this helps,

PG

http://www.pgregg.com/projects/
Copy linkTweet thisAlerts:
@jfherringauthorSep 12.2007 — PGREGG,

Thanks a lot for the suggestions. I tried all 3, just to try them out. Personally, based on code I've written in other, I like using the parentheses option. For reference, though, what is the preferred standard for PHP dev, or is there a standard? Thanks again,

jfherring
Copy linkTweet thisAlerts:
@pgreggSep 12.2007 — In your example the preferred method would be to use comma separated args to echo. e.g. echo 'foo', $var, 'bar', $etc; That is the most efficient (cpu wise).

However, I realise sometimes that is cumbersome - and its better to have simpler code than efficient code (for ease of maintenance, updating, understanding 3 years down the line) and so the <<<EOTEXT heredoc syntax may look to be the easier to use solution because you don't need to worry about escaping quotation marks in the text.
Copy linkTweet thisAlerts:
@jfherringauthorSep 12.2007 — Thanks PGREGG,

I ended up using the heredoc solution, but I may change to comma's after reading your response. Interesting that comma's are the most efficient...

Thanks again,

jfherring
×

Success!

Help @jfherring spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...