Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a called string within a php string is set?

Tags:

string

php

isset

I'm looking to call multiple strings and concatenate them into a single string. I want to be able to check that all of the strings that are being called are set, WITHOUT having to isset(); every single string that is used.

What I'd ideally like to have:

<?php

$name      = "John Smith";
$age       = "106";
$favAnimal = "pig";
$email     = "[email protected]";
$sport     = "tennis";

$userDescription = "My name is $name, I am $age years old, I like $sport.";

if(allStringsSet($userDescription)){
    echo $userDescription; //Or do something else
}

?>

You will notice that I'm not calling all of the strings, as in my application not all of the strings will be used all of the time. Also my application will be choosing from about 50 strings and I need to be able to check any are set without a pile of isset();s everywhere.

I expect that the isset checking needs to happen before the $userDescription is declared. How can I check an unknown set of strings to see if they are set?

like image 592
Samuel Hawksby-Robinson Avatar asked Feb 21 '26 07:02

Samuel Hawksby-Robinson


2 Answers

Use an object with custom getter and setter.

class CustomStrings()
{
    protected $data;

    public function __set($key, $value) {
        $this->data[$key] = $value;
    }

    public function __get($key) {
        if (isset($data[$key])) {
            return $data[$key];
        }
        return false;
    }

    public function getDescription($description) {
        // regex to find the words following : (or any other char)
        // look for the variables, and do whatever handling you want if they're not found
        // return whatever you want 
        // IF you don't want to do a regex, you can do the following
        foreach ($this->data as $key => $value) {
            $description = str_replace(":$key", $value, $description);
        }
        if (strpos(':', $description) !== FALSE) {
            return $description;
        }
        return false; // or whatever error handling you want
    }
}

$strings = new CustomStrings();
$strings->name = 'John';
$strings->age = 16;
$strings->sport = 'Football';

$description = $strings->getDescription("My name is :name, I am :age years old, I like :sport");

You will have all your variables stored in the CustomStrings object, with the proper keys, and instead of writing $name, you write :name.

You do the isset handling only once, in the getDescription function.

I didn't write the regex since I'm lazy. If you want to use : in your strings, replace it with something you won't be using.

Good luck!

like image 118
Vlad Preda Avatar answered Feb 22 '26 22:02

Vlad Preda


If you need to have ALL variables defined, PHP's isset() supports multiple values, so you could do this

<?php

$name      = "John Smith";
$age       = "106";
$favAnimal = "pig";
$email     = "[email protected]";
$sport     = "tennis";  

if(isset($name, $age, $favAnimal, $email, $sport)) {
    $userDescription = "My name is $name, I am $age years old, I like $sport.";

    echo $userDescription; //Or do something else
}

?>
like image 33
Alexandre Danault Avatar answered Feb 22 '26 23:02

Alexandre Danault