Php yerine düzgün sözdizimi?

2 Cevap

Acemi bir soru.

Nasıl yerine do:

$_SESSION['personID'] {personID}, aşağıda için:

public static $people_address = "/v1/People/{personID}/Addresses"

2 Cevap

Bu bak:

$template = "/v1/People/{personID}/Addresses";
$people_address = str_replace('{personID}', $_SESSION['personID'], $template);

echo $people_address;

çıktı:

/v1/People/someID/Addresses

EDIT: This answer no longer applies to the question after edit but I'm leaving it around for a little while to explain some questions that occured in comments another answer to this question

Bir kaç yolu vardır - . operatör muhtemelen anlamak kolay olduğunu, onun bütün amacı dizeleri bitiştirmek etmektir.

public static $people_address = "/v1/People/".$_SESSION['personID']."/Addresses"; 
//PHP Parse error:  syntax error, unexpected '.', expecting ',' or ';' 

public static $people_address = "/v1/People/$_SESSION[personID]/Addresses";
//PHP Parse error:  syntax error, unexpected '"' in

Ancak ne yazık ki emlak beyannamelerinde bitiştirmesi kullanamazsınız - sadece basit atama. Sen "dizesi yerine" biçimini kullanın ya olamaz:

Eğer sınıfın dışında statik atayabilirsiniz etrafında çalışmak - yani:

class test {
  public static $people_address;
  // ....
}

// to illustrate how to work around the parse errors - and show the curly braces format
test::$people_address = "/v1/People/${_SESSION[personID]}/Addresses";

// another (much better) option:
class test2 {
  public static $people_address;
  public static function setup() {
    self::$people_address = "/v1/People/".$_SESSION['personID']."/Addresses";
  }
}
// somewhere later:
test2::setup();