全局不工作在函数PHP 5.5.9在Ubuntu服务器14.04


Global not working in a function PHP 5.5.9 on Ubuntu server 14.04

全局变量不能在函数中工作的例子。如果我执行以下操作

$a=10;
function myfunction() {
global $a;
echo ($a+5);
}
myfunction();

它不返回任何东西但是

是这样的
function myfunction() {
//global $a;
$a=10;
echo ($a+5);
}
myfunction();

所有代码在同一页

这是问题函数,我要做什么才能让它工作

function encryptAndEncode($strIn) {
//global $strEncryptionType
      //,$strEncryptionPassword;
$strEncryptionType="AES";
$strEncryptionPassword="MyPassword";
    //** AES encryption, CBC blocking with PKCS5 padding then HEX encoding - DEFAULT **
    //** use initialization vector (IV) set from $strEncryptionPassword
    $strIV = $strEncryptionPassword;
    //** add PKCS5 padding to the text to be encypted
    $strIn = addPKCS5Padding($strIn);
    //** perform encryption with PHP's MCRYPT module
    $strCrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $strEncryptionPassword, $strIn, MCRYPT_MODE_CBC, $strIV);
    //** perform hex encoding and return
    return "@" . strtoupper(bin2hex($strCrypt));
}

发现问题:)其他人可能有这个问题。虽然这是一个单独的页面,但它在wordpress内部运行,代码直接在页面上,而不是作为要求或包含。

为了解决这个问题,我必须删除页面上设置的变量并在themes functions。php

中声明它们

似乎如果你在与wordpress有关的任何内容中运行全局变量,无论是直接在页面上还是包含文件上,它们都必须在主题函数php中预先声明。:)