douming4359 2016-06-22 20:56
浏览 17
已采纳

我怎么能这样做PHP If Statement是否正确?

I have 50 variables in php. I want to check each of them and if they true then add 2 points in a variable called $point. I am new so I write a few lines but I think I am doing wrong way.

$strenght_point = 0;

if($f_name){$strenght_point++;} 
if($l_name){$strenght_point + 2;}   
if($full_name){$strenght_point + 2;}    

How can I do it right way.Thanks

Update my full function is here... It's Codeigniter Controller Function Hope you guys understand well now

function strength_scale() {

        $user_id = $this->uri->segment(2);
        $user_name = $this->uri->segment(3);


            $query = $this->db->get_where('aoa_user', array('id' => $user_id, 'username' => $user_name));       
            foreach ($query->result() as $row){ 

                $f_name = $row->f_name;             
                $l_name = $row->l_name;             
                $full_name = $row->full_name;               
                $username = $row->username;             
                $alias_name = $row->alias_name;             
                $gender = $row->gender;             
                $country = $row->country;               
                $avatar = $row->avatar;             
                $cover_photo = $row->cover_photo;   
                $email = $row->email;   
                $skill = $row->skill;   
                $other_skills = $row->other_skills; 
                $ex_time = $row->ex_time;   
                $about = $row->about;   
                $company = $row->company;   
                $company_position = $row->company_position; 
                $phone = $row->phone;   
                $facebook = $row->facebook; 
                $facebook_page = $row->facebook_page;   
                $google_plus = $row->google_plus;   
                $twitter = $row->twitter;   
                $youtube = $row->youtube;   
                $skype = $row->skype;   
                $linkedin = $row->linkedin;                     
                $website = $row->website;   
                $latitude = $row->latitude; 
                $longitude = $row->longitude;   
                $verification = $row->verification; 

            }

        $strength_point = 0;

        if($f_name){$strength_point++;} 
        if($l_name){$strength_point + 2;}   
        if($full_name){$strength_point + 2;}    
}
  • 写回答

2条回答 默认 最新

  • donglieshe4692 2016-06-22 21:07
    关注

    Create an array with the variables instead.

    https://3v4l.org/FYTtG

    $arr = array("f_name" => true, "l_name" => true, "full_name" => true);
    
    $strength=0;
    
    Foreach($arr as $var){
       if($var) $strength = $strength+2;
    }
    
    Echo $strength;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler