PHP Notice: Use of undefined constant [duplicate]

跟風遠走 提交于 2019-12-20 07:56:27

问题


I have a strange error message after using the post to wall function. It did successfully post to the wall however i got a very weird strange error.

[Mon Dec 28 12:47:36 2015] [error] [client 5.29.224.120] PHP Notice: Use of undefined constant female - assumed 'female' in /datas/vhosts/nf.com/httpdocs/agence1948/result.php on line 178, referer: https://nf.com/agence1948/question.php [Mon Dec 28 12:47:36 2015] [error] [client 5.29.224.120] PHP Notice: Use of undefined constant male - assumed 'male' in /datas/vhosts/nf.com/httpdocs/agence1948/result.php on line 182, referer: https://nf.com/agence1948/question.php

I use this code

<?php if($_SESSION['score'] >= 15 && $_SESSION['score'] <= 20): ?>
		
		<?php if($_SESSION['gender'] == female): ?>
			<img src="img/Refaeli.jpeg"/>
		<?php endif; ?>
		
		<?php if($_SESSION['gender'] == male): ?>
			<img src="img/avidan.jpg"/>
		<?php endif; ?>
		

someone knows whats is the problem?

thank you


回答1:


male and female are strings, so enclose with quotations!

It should be:

<?php if($_SESSION['gender'] == 'female'): ?>
            <img src="img/Refaeli.jpeg"/>
        <?php endif; ?>

        <?php if($_SESSION['gender'] == 'male'): ?>
            <img src="img/avidan.jpg"/>
        <?php endif; ?>



回答2:


male and female should be enclosed with quotes as they are strings. E.g

<?php if($_SESSION['gender'] == 'female'): ?> OR

<?php if($_SESSION['gender'] == 'male'): ?>



回答3:


Two error in your code

1) You need to end first if condition

2) male and female inside quotes for comparision .Otherwise it is treated as constant

<?php if ($_SESSION['score'] >= 15 && $_SESSION['score'] <= 20): ?>

    <?php if ($_SESSION['gender'] == "female"): ?>// use quotes
        <img src="img/Refaeli.jpeg"/>
    <?php endif; ?>

    <?php if ($_SESSION['gender'] == "male"): ?>// use quotes
        <img src="img/avidan.jpg"/>
    <?php endif; ?>
<?php endif; ?>// end first condition


来源:https://stackoverflow.com/questions/34493167/php-notice-use-of-undefined-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!