Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php数组中将变量赋值_Php_Variables_Split - Fatal编程技术网

在php数组中将变量赋值

在php数组中将变量赋值,php,variables,split,Php,Variables,Split,这就是我想做的: 把一个单词分成不同的字符。输入的单词来自一个表单,每个用户可能会有所不同 将变量分配给每个字符,以便我可以分别操作它们 到目前为止,她是我的代码(不起作用)。如果这里有很多愚蠢的错误,请道歉,但我是PHP新手 <?php $word = $_POST['input']; //split word into charachters $arr1 = str_split($word); //assigning a variable to each charchter

这就是我想做的:

  • 把一个单词分成不同的字符。输入的单词来自一个表单,每个用户可能会有所不同

  • 将变量分配给每个字符,以便我可以分别操作它们

  • 到目前为止,她是我的代码(不起作用)。如果这里有很多愚蠢的错误,请道歉,但我是PHP新手

    <?php
    
    $word = $_POST['input'];
    
    //split word into charachters
    
    $arr1 = str_split($word);
    
    //assigning a variable to each charchter 
    
    $bokstaver = array();
    
    while($row = $arr1)
    {
    $bokstaver[] = $row[];
    }
    
    $int_count = count($bokstaver);
    $i=0;
    
    foreach ($bokstaver as $tecken) {
    $var = 'tecken' . ($i + 1);
    $$var = $tecken;
    $i++;
    } 
    
    ?>
    
    
    
    我希望最后得到的$tecken变量(名称为$tecken、t$tecken1、tecken2等)与输入中的字符数一样多


    一如既往,非常感谢所有帮助

    我认为这不是一个好主意,但以下是你的做法:

    <?php
    $input = 'Hello world!';
    for($i = 0; $i < strlen($input); $i++) {
        ${'character' . $i} = $input[$i];
    }
    
    你为什么要这样? 你可以选择:

    $word = 'test';
    echo $word[2]; // returns 's'
    echo $word{2}; // returns 's'
    $word{2} = 'b';
    echo $word{2}; //returns 'b'
    echo $word; // returns 'tebt'
    ...
    

    您不需要为每个字母创建单独的变量,因为所有字母都在一个数组中。然后,您只需索引到数组中,即可取出每个字母

    我会这样做的

    //get the word from the form
    $word = $_POST['input'];
    
    //split word into characters 
    $characters = str_split($word);
    
    
    //suppose the word is "jim"
    //this prints out 
    // j
    // i
    // m
    
    foreach($characters as $char)
        print $char . "\n"
    
    
    //now suppose you want to change the first letter so the word now reads "tim"
    //Access the first element in the array (ie, the first letter) using this syntax
    $characters[0] = "t";
    

    动态创建变量名会带来麻烦。我强烈建议使用数组。有什么特别的原因不能简单地使用str_split()创建的数组吗?它说:致命错误:不能使用[]在C:\xampp\htdocs\quick\hangmanindex.php第15行读取。谢谢msgmash.com提供您的asnwer,但您能说得更具体一点吗。这是一个文字游戏应用程序,我希望建立。我只是觉得PHP会是一种合适的语言,但正如我所说的,我对PHP som很陌生,也许PHP不是一个好的选择。这是OP要求的,但这样做总是一个坏主意。谢谢你的回答Jon!但为什么这是一个坏主意,还有什么更好的方法呢。@user1009453这取决于您实际想要做什么。如果您需要一组字符,则任何字符串都已经存在。(正如您在本例中所看到的)这是一个我希望构建的简单文字游戏应用程序。这个想法是player1选择一个单词,player2通过一次猜一个字符(他可以确定这个单词包含多少个字母)来尝试猜单词。如果player2获得匹配,那么单词中的字母对他来说是可见的。我只是觉得我必须把原来的单词分成几个单独的变量,这样这些变量就可以单独处理了。也许我错了。@user1009453您可以通过索引单独访问字符串的字符:
    echo$input[2]
    将输出第三个字符。值得注意的是,
    $var[0]
    是执行此操作的“正确”方法
    $var{0}
    语法在PHP6中被非正式地反对用于字符串,尽管阅读手册时看起来情况不再如此,因为它与5.3 trunk.str_split()合并在一起,语法仍然相同