如何使用php中的字符串内的条件 [英] How to use if condition inside string in php

查看:82
本文介绍了如何使用php中的字符串内的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法使用这样的东西:

I want to know if there is a way to use something like this:

$t1=1;
$t2=2;
$t3="$t1>$t2";

if($t3)
   echo "Greater";
else
   echo "Smaller";

这将评估为真,因为$ t3是一个字符串,但这是错误的!

This will evaluate to true as $t3 is a string, but that's wrong!!!

那么有没有办法在字符串中包含if条件。

So is there a way to include the if condition inside string.

我听说我们可以使用eval:
PHP - 如果字符串内的条件

I heard that we can use eval for this: PHP - if condition inside string

但是这怎么可能???

But how is this possible???

推荐答案

看起来它应该可行,给定PHP 评估页面。

This looks like it should work, given the PHP eval page.

$t1=1;
$t2=2;
$t3="return $t1>$t2;";

if(eval($t3))
   echo "Greater";
else
   echo "Smaller";

这篇关于如何使用php中的字符串内的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆