Home > Backend Development > PHP Tutorial > PHP 把if里面的变量引用到else 有这样的方法吗?该怎么处理

PHP 把if里面的变量引用到else 有这样的方法吗?该怎么处理

WBOY
Release: 2016-06-13 13:27:42
Original
863 people have browsed it

PHP 把if里面的变量引用到else 有这样的方法吗?
如题,谢谢

------解决方案--------------------
不明白你的意思
------解决方案--------------------

PHP code

$test = 1;
if($test){
   $test = 2;
}else{
   #这里想要输出$test==2 ?
   echo $test;
   #除了重新赋值,想不到其他办法了,因为这样的话,if,else就没有使用的必要了
}
<br><font color="#e78608">------解决方案--------------------</font><br>if($a++){<br><br>}else{<br>  echo $a;<br>}
<br><font color="#e78608">------解决方案--------------------</font><br>利用goto跳进if里面去……走完赋值语句跳回来……<br><br>有点X
<br><font color="#e78608">------解决方案--------------------</font><br>你和我问的那个'if 结构有点乱'的问题是一样的,别想了,用session吧,要不实在不好弄,我在表单里传递进来的值都无法办到数据回传.<br><br>但是我这几天实验的结论是,在IF 中定义的普通变量无法传出,如果找到策略了,还希望贴出来分享下.我也学学.<br>思路:<br>先session_start()<br>然后$_session['your val']=$val;<br>下面代码输出的是$_SESSION['newval']=3,$str= ;<br> echo '$str='.$str;<br>
Copy after login
PHP code

session_start();  
global $str;
$test='vcd'  ;
  if ($test==='vc') { 
$str=$_SESSION['newval'] =3 ;   
  $str=$_SESSION['newval']  ;  
   echo '$str='.$str;  
  }
else{
 // session_start();   
  echo '$_SESSION['.'newval'.']='. $_SESSION['newval'] ;
  echo '$str='.$str; 
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template