如何创建函数以添加到现有信息中


How do I create a function to add to existing information?

我现有的代码有一个字符串。我需要能够创建一个函数来添加到现有的字符串中。我很难在php中做到这一点。我是新来的,我以前查阅的所有资料都没有给我任何帮助。

当一切都说了又做了,这就是我想要的:

一个函数,用于将一个新词添加到包含字符串的现有变量中。(以下是现有的单词代码)

$words = 'Apple Sauce, Tasty Chicken, New Monkey, Left Right';

以下是我能想到的:

function newWord($word){
   $newAlpha = 'Time Table';
   if ($newAlpha > 0){
      echo $newAlpha => $words; 
  }
}

我知道这是不对的,但我对php和mysql还很陌生。可能值得注意的是,最终我需要将该函数插入到一个存储$words的数据库中,但如果有人能帮助我,那将是一个额外的奖励。

只需将其与字符串连接即可:

function newWord($word, $words){
   if($word != ''){//or any other check you want
       return "$words, $word"; 
   }
   return $words;
}

用法:

$newAlpha = 'Time Table';
$words = newWord($newAlpha,$words);//now $words has $newAlpha appended onto the end with comma and space

你可以在这里看到它的作用:

http://sandbox.onlinephpfunctions.com/code/436189dc45208fb78bdfa4262772600559f29d44

您可以简单地使用concat运算符.

$words = 'Apple Sauce, Tasty Chicken, New Monkey, Left Right';
$added_words = $words .'new_word_one, new_word_two, etc...';
var_dump($added_words); 
// produces (string)"Apple Sauce, Tasty Chicken, New Monkey, Left Right new_word_one, new_word_two, etc..."
$words = 'Apple Sauce, Tasty Chicken, New Monkey, Left Right';
$new_plus_old_words = "$words new_word1, new_word2, new_word3";