如何在if条件下定义变量

时间:2017-04-21 10:59:26

标签: php html

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<link rel="stylesheet" href="css/bootstrap.min.css">
<title>Submitted</title>
</head>

<body>
<?php
$employeeName = $_POST["txt_name"];
$employeeEmail = $_POST["txt_email"];
$employeeCompany = $_POST["txt_cmp"];
$employeeDesignation = $_POST["slc_dsgn"];
$employeeSalary = $_POST["txt_slry"];
$employeeStatus = $_POST["status"];
$employeeReason = $_POST["msg_rsn"];


?>
<div class="container">
<table class="table table-inverse">
 <thead>
<tr>
  <th>#</th>
  <th>Employee Name</th>
  <th>Email Address</th>
  <th>Company</th>
  <th>Designation</th>
  <th>Salary</th>
  <th>Status</th>
  </tr>
 </thead>
 <tbody>
  <tr>
    <th scope="row">1</th>
    <td><?php echo($employeeName)?></td>
  <td><?php echo($employeeEmail)?></td>
  <td><?php echo($employeeCompany)?></td>
  <td><?php echo($employeeDesignation)?></td>
  <td><?php echo($employeeSalary)?></td>
  <td><?php echo($employeeStatus)?></td>
</tr>

 </tbody>
 </table>
 <?php
 if($employeeSalary > 20000){
 $percentage = 35;
  $loan = ($percentage / 100) * $employeeSalary;

  }
  ?>

  <?php
  if($employeeSalary > 30000){
  $percentage = 25;
   $loan = ($percentage / 100) * $employeeSalary;

  }
  ?>

  <?php
  if($employeeSalary > 40000){
  $percentage = 15;
  $loan = ($percentage / 100) * $employeeSalary;

  }
  ?>
   <div class="text-center display-4 bg-faded justify-content-around"> your Loan will be <?php echo($loan)?> as per your current salary </div>

   <p class="blockquote text-center justify-content-around"> <?php echo($employeeReason);?>
   <span class="blockquote-footer">Reason</span>
   </p>
   </div>
   </html>

这是我的代码当我在本地主机上打开我的页面时,我对php不熟悉它会说$ $ undefine变量甚至我在每个单独的条件下定义它然后我用它来获取百分比但是我还试图把这个变量全局化,但是我没有根据我的条件改变它的价值我不知道如何解决这个问题

P.s这个问题是如此变化,我已经尝试了很多找到类似的但我无法得到。

1 个答案:

答案 0 :(得分:0)

尝试有条件地定义它:

if(!isset($percent)){
  $percent = 0;
}

这将安全地定义变量,但不会扰乱程序中的任何内容。