PHP"如何包含' - '只有当变量存在时?

时间:2016-05-05 20:39:06

标签: php

    public class Game

{

 public static void main(String[] args)

 {
Scanner scan = new Scanner(System.in); // Creates scanner object.
Random numb = new Random(); // Creates an instance of the random class.

String newgame = "y";

while (newgame.equalsIgnoreCase("y")) {
  int count = 0; // Placeholder for the guess counter.
  int guess = -1; // Placeholder for users guess.
  int answer = numb.nextInt(100) + 1; // Generates a random number for the game.
  int sentinel = 0; // Placeholder for players answer as to whether they want to play again or not.

  while (guess != sentinel && guess != answer) // Loop that ends when user enters a zero.
  {
    System.out.println("Enter a number between 1-100 or 0 to quit");
    guess = scan.nextInt();
    count++;

    if (guess < answer && guess > 0) {
      System.out.println("Your guess is too low, guess again");
    } else if (guess > answer) {
      System.out.println("Your guess is to high, guess again");
    }

    else if (guess == answer) {
      System.out.println();
      System.out.println("You guessed correctly, you win!!!");
      System.out.println("It took you " + count + " guesses");
    }
  }
  System.out.println();
  System.out.println("Play another game: y or n?");
  newgame = scan.next();
}
}
}

如果$url = 'http://www.example.com/'.$type.$q.'number-'.$L.','.$country.'.html $type之后包含$q,则示例:

-

但是当例如$type = 1 $q = 2 http://www.example.com/1-2-number-fl,us.html 不存在时:

$type

更新:如果 $ type = 1 - $ q = 1 - ,那么你可以阻止这种情况发生: http://www.example.com/1--2--number-fl,us.html < /强>

5 个答案:

答案 0 :(得分:2)

另一种方式:

if(isset($type)) { $array[] = $type; }
if(isset($q))    { $array[] = $q;    }
$array[] = 'number';
if(isset($L))    { $array[] = $L;    }

$url = "http://www.example.com/".implode('-', $array).",$country.html";
  • 检查变量是否设置为isset()
  • 如果是,请将它们添加到数组
  • 由于number是您想要的固定字符串,因此将其添加到数组
  • implode()加入-的数组,以便在网址
  • 中使用

您可以将isset()替换为!empty(),但0被视为空,因此无法在网址中使用。

答案 1 :(得分:0)

如果两者都存在则使用isset()然后构建字符串

if (isset($type, $q)) {
  $result = $type .'-'. $q;
{

答案 2 :(得分:0)

检查$ type和$ q是否为空是很重要的 - 仅仅检查它们是否已设置是不够的。您还需要覆盖值为整数0的情况,因为PHP将0视为空字符串,这会导致空(0)返回true。

你可以选择这样的东西:

if ( ((int) $type !== 0 && !empty( $type )) && ((int) $q !== 0 && !empty( $q ))) {
    $result = $type . '-' . $q;
}
elsif ((int) $type !== 0 && !empty( $type )){
    $result = $type;
}
else {
    $result = $q;
}

$url = "http://www.example.com/{$result}-number-{$L}{$country}.html";

答案 3 :(得分:0)

首先检查变量是否已设置且不为空。为每个变量添加“ - ”。

$url = 'http://www.example.com/'.$type.$q.'number-'.$L.','.$country.'.html';
// declare a variable
$result = '';
// check for the variable type and add '-'
if(isset($type) && ($type!='')){
    $result=trim($type,'-').'-';
}
// check for the variable q and add '-'
if(isset($q) && ($q!='')){
$result.=trim($q,'-').'-';
}

$url = 'http://www.example.com/'.$result.'number-'.$L.','.$country.'.html';

答案 4 :(得分:-1)

检查isset()

if(isset($type))
    http://www.example.com/1-2-number-fl,us.html
else
    http://www.example.com/2-number-fl,us.html