Avatar of deanlee17
deanlee17
 asked on

PHP IF statement error

Hi guys,
Can anyone see why my ELSE statement is erroring?

<?if(sizeof($row_product['multis']) > 0){?>
	
<?if(substr($row_product['product_code'],0,4) == "qte_"){?>
	
<div class="order_submit">
<form name="testform" method="post">
<input type="button" id="quote_button" class="quote_button" name="quote_btn" value="Quote me" />
</form>	
</div>
<?}?>
	<? else {print_r(substr($row_product['product_code'],0,6)); } ?>
<?}?>

Open in new window


Many Thanks,
Dean
PHPWeb Languages and StandardsScripting Languages

Avatar of undefined
Last Comment
Member_2_248744

8/22/2022 - Mon
mcuk_storm

You are closing the if brace inside a different code block then expecting to continue the if statement with the else in a new code block which is throwing PHP.

<?}?>
	<? else {print_r(substr($row_product['product_code'],0,6)); } ?>

Open in new window


This will work fine:
<?} else {print_r(substr($row_product['product_code'],0,6)); } ?>

Open in new window



I can't see any reason why you would want to separate it
Mark Brady

Your code is very hard to read. Why do you use open and close tags everywhere? That is not necessary at all. Also try to format your code better to help people to read it. Indent each line inside a code block with a tab at least.
Cornelia Yoder

You have the wrong direction bracket on line 11.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Cornelia Yoder

Here is correct code as I think you mean it to be:

if(sizeof($row_product['multis']) > 0)
{

   if(substr($row_product['product_code'],0,4) == "qte_")
   {

?>
<div class="order_submit">
<form name="testform" method="post">
<input type="button" id="quote_button" class="quote_button" name="quote_btn" value="Quote me" />
</form>
</div>
<?

   }
   else
      print_r(substr($row_product['product_code'],0,6));
}
Cornelia Yoder

@mcuk_storm:  You can open and close php as much as you want and mix html into it exactly as this asker is doing.  It just makes for hard to read, hard to debug code.
mcuk_storm

@yodercm This seems a very wrong place to discuss this but as you brought it up, if you consider how PHP compiles this down to op code anything not in a php code block will be converted to an echo so
<?php $a=1; if ($a==1) { ?>
a
<?php } else { ?>
b
<?php } ?>

Open in new window


would effectively be (though i'll spare you the op code)
$a=1; if ($a==1) { 
echo "a";
} else { 
echo "b";
}

Open in new window


Now consider what would happen with this which is akin to the example in this question
<?php $a=1; if ($a==1) { ?>
a
<?php } ?>
<?php else { ?>
b
<?php } ?>

Open in new window


this would effectively be
$a=1; if ($a==1) { 
echo "a";
} echo "\n"; else { 
echo "b";
}

Open in new window


Now do you see why this is incorrect?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
mcuk_storm

If you want to see what happens to text outside php blocks i have posted a quick example of the op code at: http://pastebin.com/MKnhvrqf
Cornelia Yoder

No, I don't see any incorrect. I agree it's inefficient.
mcuk_storm

You still don't see why trying to compile
} echo "\n"; else { 

Open in new window

would never work? seriously? Effectively trying to put a statement between the closing brace of an if and its corresponding else keyword?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Cornelia Yoder

I think you are confused about how this works, I do it all the time in many many scripts and ways, and never had a problem.
mcuk_storm

@yodercm I'm not taking this any further but my closing statement is, yes you can intersperse html between PHP blocks but not when you break in between a closing control block brace for an if statement and a corresponding else statement associate with it. Which is what all of this question is ultimately about.

The fact the script won't run or compile when you do this should be a bit of a give away, and give that this is exactly what is stated in the original question, if you still don't believe me then just try and run this:
<?php $a=1; if($a==1) {echo "1";} ?> 
<?php else { echo "2"; } ?>

Open in new window

You will get: PHP Parse error:  syntax error, unexpected 'else' (T_ELSE) in /home/???/test5.php on line 2

I don't have access to anything older than PHP 5.2 but from 5.2 to 5.4 this will not run.

Unless the author has any further queries i will not respond further to this question.
Cornelia Yoder

I ran your code copy/pasted from above:


<?php $a=1; if ($a==1) { ?>
a
<?php } else { ?>
b
<?php } ?>


and its output was

a

Where is the problem?


This may have the effect of an echo, but it's not the same as putting an echo statement into the code.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Cornelia Yoder

Regardless of how it works, what the asker needs is approximately this:

if(sizeof($row_product['multis']) > 0)
{

   if(substr($row_product['product_code'],0,4) == "qte_")
   {

?>
<div class="order_submit">
<form name="testform" method="post">
<input type="button" id="quote_button" class="quote_button" name="quote_btn" value="Quote me" />
</form>
</div>
<?

   }
   else
      print_r(substr($row_product['product_code'],0,6));
} 

Open in new window

deanlee17

ASKER
Wow, ive been lavished with such attention lol.

To be honest I am not a PHP programmer and I believe the way I originally coded this was just copying what I had seen someone do in my previous place of work.

I looked at your first post example and it works, However I still dont quite understand, you say 'You are closing the if brace inside a different code block then expecting to continue the if statement with the else in a new code block which is throwing PHP' But your solution still essentially closes the IF brace in a different block than the original one where the IF statement originates (as it will obv need to).

Obv I am not saying you are incorrect, as obv you are not, just trying to get my head around it.

Thanks.
mcuk_storm

Sorry for the long winded posts, the issue is to do with how the if and else are split between php code blocks "<? } ?><?php else {" which was causing the issue.

If the else wasn't present and you had started some other php code in it's place it wouldn't have been an issue, it is purely where the split between the php blocks is occurring in this example, in most situations this wouldn't occur as there isn't really a reason to split the php code between php blocks at that point.

I suppose with no whitespace between the php blocks it could be expected to work (but doesn't) so in this respect may loosely be thought of as a bug, but i'm not familar enough with how the code is parsed by the compiler in PHP to be able to determine this.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Cornelia Yoder

You really don't need to worry about going in and out of php, and having html between php blocks doesn't cause a problem, even in an if-then-else structure.  I do it all the time.

However, you should take out all of the switches <?  ?> that aren't absolutely necessary, as I did in the code I posted.
Chris Stanyon

When coding in and out of php / html blocks it's often easier to use the alternative control structures of colons and 'end' statements and forget about the curly brackets altogether. It also helps to indent your code so you can see exactly what's going on. Finally, if using PHP then make it obvious - you're currently using shorttags (<?) which at some point will stop working and your code will break.

<?php
if (sizeof($row_product['multis']) > 0): 
	if(substr($row_product['product_code'],0,4) == "qte_"): ?>

		<div class="order_submit">
			<form name="testform" method="post">
				<input type="button" id="quote_button" class="quote_button" name="quote_btn" value="Quote me" />
			</form>	
		</div>

	<?php endif;
else :
	print_r(substr($row_product['product_code'],0,6));
endif;
?>

Open in new window

Cornelia Yoder

>>>>>you're currently using shorttags (<?) which at some point will stop working and your code will break.

With the several billion webpages using shorttags, how soon do you think the php developers will be suicidal enough to eliminate their use? :):)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Stanyon

PHP Developers - probably not | Server admins - highly likely ;)

<?php is always available
<? depends on config!! and is NOT RECOMMENDED

Default is short_open_tags = OFF
Short tags may cause confusion if used with XML
Only reason to use - laziness

Your choice...Why take the risk?
deanlee17

ASKER
Wow, what insightful posts :)
mcuk_storm

Whilst not many web hosts are providing PHP 5.4 yet, but as of 5.4 short open tags are always on:
http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

(Though it looks like it may only be the short hand echo version "<?=" will need to check if that is short open tags in general)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Chris Stanyon

as of 5.4 short open tags are always on
unless they're not!

While the default in 5.4 may be ON, a server admin may turn them off. It's simply not considered best practice.
Cornelia Yoder

No reputable webhost is going to crash the millions of webpages they host by turning shorttags off, unless they want to go out of business.

All of this is beyond the scope of the question.  While ChrisStanyon and RayPaseur have slightly modified my solution by indenting or putting the html into an echo, the basic answer is still the same, get rid of the extraneous php on/off tags and you will much more easily see the minor syntax error.
Member_2_248744

Hello and Questions for deanlee17, , ,  I looked at the code you start this question with, and the comments so far, , ,

as for my views, , you do Not seem to have a good idea of what you want your PHP code to be doing (what the final result Display on a web page) as you use several un-useful PHP methods for this, as experts have already told you about, , , and your response comments have been an encouragement for more comments, but not giving any helpful info to get real useful PHP help to you.

your first line of code uses a PHP of  sizeof(  ) , , this should be the PHP  count( )  for arrays, where ever you got your code info from , you should use another more better code source.

you have this = <form name="testform" method="post">, , which can work, but could be a better HTML form declaration. But regardless of the FORM declare, , your FORM does NOT have any way to SUBMIT, there is NO html for =
 <input type="submit" name="testSub" value=" Do This ">

you do Have ONE button in your form HTML, but this Button does Not seem to have any way to do any thing if it is clicked and important for a form HTML it has NO significance in PHP POST data even if you could SUBMIT the Form.

also you have =
print_r(substr($row_product['product_code'],0,6));

which seems like the PHP print_r( ) is for a DEBUG kind of thing, but you do not seem like you know enough PHP to do debugging?

you do not use any of the data in the  $row_product  to add INPUTS or HIDDEN in your form which seems like the usual for a PHP coder to do.

Please do not take my statements as a criticism of your code, I am just trying to help you learn PHP and HTML output, by letting you know that even if you do not have the error you first speak of, your PHP and html FORM code may still not be useful for you, if you do not learn and use some more better code methods for making a dynamic PHP form on code.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
deanlee17

ASKER
Slick812,

Your statement is clearly a bit of a critisism, but thats cool. Im NOT a PHP developer, im not even a web developer anymore and have not been for quite some time. But obv I do really appreciate all the help and comments. The reason I am in this position is because I am creating an ecommerce site and the content management system that is provided to me has pre written code, which in the main works fine, however im just tweaking a few little bits of CSS and this small part of PHP. So let me work through your post...

your first line of code uses a PHP of  sizeof(  ) , , this should be the PHP  count( )  for arrays, where ever you got your code info from , you should use another more better code source.
This was pre written. I will take that onboard tho.

<form name="testform" method="post">
Essentially this isnt required as there is jquery picking up the button click event in this case.

print_r(substr($row_product['product_code'],0,6));
Yes I was 'kind of' degugging and trying to return the first X digits of the product code for my own reference.

Thanks for your post.

Essentially I now believe I should have just...

<?php
if (substr($row_product['product_code'],0,4) == "qte_")
    {
        echo '<p> ECHOED OUT ANYTHING OUTSIDE THE PHP ARENA </P>';
    }
?>

Open in new window

Member_2_248744

OK, thats more of an Idea to me, of what you may be doing,  and to have just a single <?php  seems better.
your code example will work, as far as I can tell. But I will say that many CMS I have had to deal with,  do best if you use their System tools and operations, to get the "Tweeks" you need, and some do not do well at all if you write your own PHP code to output on the page, and can "Bump" into the CMS operations.

May the Luck of the CMS Tweeks be with You!