Gizlemek veya PHP ile metin ve etiketleri görüntülemek nasıl?

3 Cevap

Ben şehir devleti ve ülke mevcut olmasa metin Yer gizlemek ne kadar merak ediyordum ve nasıl değişkenlerin herhangi mevcut olsaydı o görüntüler izin istiyorsunuz?

<p>Location:
<?php
 if (!empty($city))
     {
    	echo $city .',';
     }

 if (!empty($state))
     {
    	echo $state;
     }

 if (!empty($country))
     {
    	echo ' ' . $country;
     }
?>    
</p>

3 Cevap

De bir PHP bloğunun sarın:

<?php

// Check if any of them is not empty.
if(!empty($city) || !empty($state) || !empty($country)){
    echo "<p>Location:";
    if (!empty($city))
    {
        echo $city .',';
    }
    if (!empty($state))
    {
        echo $state;
    }
    if (!empty($country))
    {
        echo ' ' . $country;
    }
    echo "</p>";
}

Kullanın:

<?php
 $address = "";
 if (!empty($city))
     {
    	$address_parts[] = $city;
     }

 if (!empty($state))
     {
    	$address_parts[] = $state;
     }

 if (!empty($country))
     {
    	$address_parts[] = $country;
     }
$address = implode( ", ", $address_parts);
if (!empty($address)) 
{
 echo   "<p>Location: " . $address . "</p>";
}
?>

Adres kurmak ve adresi hala yerini görüntülemeden önce boş ise o zaman sınayın.

Edit
Changed how the address is built up. Rather than concatenate everything, the address parts are added to an array and then imploded with the appropriate separator.

tam olarak aynı ama benzer bir sorun değil, ben bu yöntemi kullandı.

Bu da size yardımcı olur olabilir :)

<?php
function publish($buffer){
global $location;
if (empty($location))
	$buffer="";
return $buffer;
}
ob_start("publish");

$location=$_GET["location"];
echo "Location: $location";

ob_end_flush();
?>

test the url with location parameter and without a parameter. And you can find more detail about this in php.net output control functions