Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.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/3/heroku/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_Arrays - Fatal编程技术网

Php 如果他在函数中创建了数组,如何打印?

Php 如果他在函数中创建了数组,如何打印?,php,arrays,Php,Arrays,在函数测试()中创建的数组 然后我想在页面test.php上打印它们的内容 我的代码被删除: conf.php function test(){ $str= array( 'items' => array( 0 => array( 'title' => 'Title', 'category' => 'Category name',

在函数测试()中创建的数组

然后我想在页面test.php上打印它们的内容

我的代码被删除:

conf.php

function test(){
$str= array(
        'items' => array(
                0 => array(
                        'title' => 'Title',
                        'category' => 'Category name',
                        ),
                1 => array(
                        'title' => 'Title',
                        'category' => 'Category name',
                        ),
        ),
        'details' => array(
                'firstname' => 'firstname',
                'lastname' => 'lastname',
        ),
        'Id' => $Id,
        'OrderId' => 'fsdfsfdsrew'
);


$json = json_encode($str);
$base64 = base64_encode($json);

$sig = signMessage($base64, $secretPhrase);
}
require_once("conf.php");

test();
print_r($str);
print_r($json);
print_r($base64);
print_r($sig);
test.php

function test(){
$str= array(
        'items' => array(
                0 => array(
                        'title' => 'Title',
                        'category' => 'Category name',
                        ),
                1 => array(
                        'title' => 'Title',
                        'category' => 'Category name',
                        ),
        ),
        'details' => array(
                'firstname' => 'firstname',
                'lastname' => 'lastname',
        ),
        'Id' => $Id,
        'OrderId' => 'fsdfsfdsrew'
);


$json = json_encode($str);
$base64 = base64_encode($json);

$sig = signMessage($base64, $secretPhrase);
}
require_once("conf.php");

test();
print_r($str);
print_r($json);
print_r($base64);
print_r($sig);
请告诉我为什么代码不起作用

请告诉我为什么没有打印
$str
$json
$base64
$sig

怎么做


最好没有全局参数。

如果不将它们作为函数返回值返回,则无法执行此操作。在PHP中,函数中声明的变量(在本例中,您试图打印的数组)仅在该函数的范围内可用,除非您使用global关键字将其声明为全局变量

以下是PHP中变量作用域的详细信息:

您可以构造一个更大的数组来包含这些数组,并从test()函数返回它们:

 function test(){
     //function code here....
     ////...

     $results = array('str'=> $str, 
                      'json'=> $json, 
                      'base64'=>$base64, 
                      'sig' => signMessage($base64, $secretPhrase)
                  ) ;

      return $results;
  }
那么就这样称呼它:

  $results = test();
  print_r($results['str']);
  print_r($results['sjson']);
  print_r($results['base64']);
  print_r($results['sig']);
有很多方法可以做到这一点: 首先,如果要在其他类上使用,则必须返回该值。 在测试中,您可以执行以下操作:

$something = new Conf();
$someelse = $something->test();
echo $someelse;

返回$str并调用为
$str=test()
@MichaelBerkowski我想打印所有数组,只有当你需要返回所有数组时(刚刚注意到它们都是在函数中创建的),你才需要将它们作为数组返回。
return array('str'=>$str,'json'=>$json','base64'=>$base64,'sig'=>$sig)
@MichaelBerkowski,然后如何打印它们?我知道这一点,但如果我创建了
返回数组('str'=>$str,'json'=>$json','base64'=>$base64,'sig'=>$sig),我想知道如何打印它们非常感谢我的新知识)在您的Conf.php的第一行中,使类Conf{methods code}@Guerra在php中使用
->
而不是
调用类的非静态方法。@Ray您是对的,对不起。Java编程工具