• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] : Is there a way to make inline handiling + Some help =)

Flourence

New Member
Joined
Sep 22, 2009
Messages
60
Reaction score
4
I donno whatever it is called but is there a way to do that thing in lua in php :

Lua:
local n = 1

local f = ( n==1 and "true" or "false")

This case variable f would be => "true"

can i do same in php ? something like: ( i know this don't work , but how can i do it the right way)

PHP:
$n=1;
$f = ($n==1 AND "true" OR "false");
 
You can.
PHP:
$f = ( in_array( 'value', $array ) ? TRUE : FALSE );
$f = ( $str == "Hey There!" ? TRUE : FALSE );
$f = ( $n == 1 && $str == "Hey There!" ? TRUE : FALSE );

EDIT: Oh, by the way - they're called conditional statements if you would like to look further into them. :)
 
Last edited:
humm ,more question :p

Why the newline character not work here?

PHP:
<?php

$ar = array(
            "Sengafora" => "Shops",
            "Dubai" => "Buildings",
            "Egypt" => "Protests"
);
$str = "Countries & Specialities : \n ";
$i = 1;
foreach($ar as $country=>$special){
    $delim =  ($i < count($ar) ? " \n " : ".");
    $str = "$str <b>$country</b> is specialized with <b>$special</b>$delim" ;
    ++$i; 
}
  echo $str;
?>

Code:
Countries & Specialities : Sengafora is specialized with Shops Dubai is specialized with Buildings Egypt is specialized with Protests.

It doenst make a new line!
 
It does make a silent new line. If you view the source code of your document through your browser, it'll be on separate rows.
You'd have to use the HTML break row ( <br /> ) element in order to "really" break the line.
 
humm, i have made this but i dont know why it dont work the page is just like server error no indication to anything :
PHP:
 <?php

$key = array("1","2","3","4","5","6");

function table_remove_value($val,&$arr)
{
    $res = false;
    $i = 0;
    $len = count($arr);
	
    foreach($arr as $v){
	
           if ($v == $val){
                    $res = true;   
                    break;
					
           }else {i++;}   
    }
    if ($res == false ){return false;}
	else{
        if (($i+1) == $len){return array_pop($arr);}
		
        else{array_splice($arr,$i,-($len - ($i+1)));}
          return true;
		  
    }
    return true;
}

if (table_remove_value("4",$key)) {

     foreach($key as $values){echo "$values </br>";}
    
} else{echo "nooob";}
?>
 
Ah yes, I often forget the variable prefixes too, since I've moved from Lua to PHP.

Flourence, also remember that you can always ommit the { } brackets there's only 1 statement inside, or if the only statement is a loop.
 
I'm not much better when it comes to semi-colons. They tend to be forgotten.

By the way, here's a more efficient way of doing what your code does (in case you wish to study it):
PHP:
<?PHP

	$keys = array( 1, 2, 3, 4, 5, 6 );
	$valueToRemove = 4;
	
	if ( !in_array( $valueToRemove, $keys ) )
	{
		die( 'Could not find/remove the key with a value of '.$valueToRemove );
	}
	unset( $keys[array_search( $valueToRemove, $keys)] );
	
	foreach( $keys as $key )
	{
		echo $key.'<br />';
	}

?>
 
Is there a way to show me where is the error i mean in which line ??
until i get to a way to do so i have this also and not working
PHP:
<?php

function isAlphb(&$var)  ## Function
{
        $res = true;
        $explo = str_split($var);
        foreach($explo as $key)
        {   
            if((int)$key)
                $res = false;
        }    
        return $res;
}
  
class Test  ## OOP
{
    private $name;
    private $result;
    
    public function setName($name)
        {
            if  (!is_string($name)){
                $this->name = "Unknown";
                 $this->result = "Your name wasn't a string";}
            elseif (!isAlphb($name)){
                $this->name = "Unknown";
                $this->result = "You can only include alphabetical characters in your name";}
            else
                $this->name = $name;
        }
        
    public function showName()
            {return $this->name ;}
    
    public function showResult()
            {return $this->$result;}
}

##checks starts
$employer = new Test();  
$employer->setName("Kari2m");
$name = ($employer->showName());

if ($name == "Unknown")
    die("Name validation error : ".$employer->showResult().".");
else
    echo "Your name is : ". $name.".";

?>
 
PHP:
{return $this->$result;}
Should be
PHP:
{return $this->result;}
Enable errors in your php.ini file and restart your webserver to see where your errors are located.
 
Humm, can any one tell me what is the use of interface, i mean is there a great use to them and the abstract classes too? and what is the difference between the 2 of them?
 
i can only tell you that interface isnt consider as a class, and the only thing i can tell you about both that they are used so you dont forget one of the methods in the class you implement them in, as it would give you error if not all methods defined in abstract class or interface not declared in the current class. Thought there is a bit of difference in using them : As you can inherit more than one interface, so if your object inherit from number of sources you should use interface , unlike the abstract class which you can only inherit one abstract class, so it is obvious that you can define the behavior of the methods in the abstract class (if you know that classes will share common ones), but you can't include behavior in the interface.
 
Back
Top