【问题标题】:Unable to print items from an array in php无法从php中的数组打印项目
【发布时间】:2018-09-19 20:27:04
【问题描述】:

我的代码应该通过将每个单词的首字母大写来固定课程名称。然后我需要在全局数组中对它们进行排序并用它们打印出一条消息。我想我已经正确地完成了所有这些,但是当我尝试打印出变量时,我的浏览器中什么也没有,也没有错误消息消失。我想知道如何在数组中打印这些更改的变量。

<?php
$course1 = "advanced web development";
$course2 = "mobile app development";
$course3 = "info systems with business intell";

function fixCourseName($courseName)
{
$courseName = ucwords($courseName);
return $courseName;
}

$GLOBALS['CIS475'] = fixCourseName ($course1);
$GLOBALS['CIS360'] = fixCourseName ($course2);
$GLOBALS['CIS429'] = fixCourseName ($course3);

print_r $GLOBALS['CIS475'];

?>

【问题讨论】:

  • 投票以拼写错误结束

标签: php arrays printing superglobals


【解决方案1】:

你有一个语法错误——print_r是一个函数;您想将$GLOBALS['CIS475'] 作为参数传递。

print_r $GLOBALS['CIS475'] 替换为print_r($GLOBALS['CIS475']),您的代码将按预期工作。

这可以看到 here

【讨论】:

    【解决方案2】:
    <?php
    $course1 = "advanced web development";
    $course2 = "mobile app development";
    $course3 = "info systems with business intell";
    
    function fixCourseName($courseName)
    {
    $courseName = ucwords($courseName);
    return $courseName;
    }
    
    $GLOBALS['CIS475'] = fixCourseName ($course1);
    $GLOBALS['CIS360'] = fixCourseName ($course2);
    $GLOBALS['CIS429'] = fixCourseName ($course3);
    
    print_r($GLOBALS['CIS475']);
    
    ?>
    

    【讨论】:

    • 可以添加一些解释吗?
    猜你喜欢
    • 2021-05-20
    • 2017-10-08
    • 2020-09-29
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多