Maison > Article > développement back-end > php 获取全局变量的简单代码_PHP教程
<span 1</span> <?<span php </span><span 2</span> <span /*</span><span * </span><span 3</span> <span * 获取全局变量 </span><span 4</span> <span * by www.jbxue.com </span><span 5</span> <span */</span> <span 6</span> <span function</span> cleanGlobal(<span $global_array</span>, <span $arg</span>, <span $specialchars</span> = <span true</span>, <span $default</span> = <span null</span><span ) { </span><span 7</span> <span 8</span> <span if</span>(<span key_exists</span>(<span $arg</span>, <span $global_array</span>) && <span $global_array</span>[<span $arg</span>] != <span null</span> && <span $global_array</span>[<span $arg</span>] != ""<span ) { </span><span 9</span> <span if</span>(<span $specialchars</span><span ) { </span><span 10</span> <span return</span> <span htmlspecialchars</span>(<span $global_array</span>[<span $arg</span><span ]); </span><span 11</span> } <span else</span><span { </span><span 12</span> <span return</span> <span $global_array</span>[<span $arg</span><span ]; </span><span 13</span> <span } </span><span 14</span> } <span else</span><span { </span><span 15</span> <span return</span> <span $default</span><span ; </span><span 16</span> <span } </span><span 17</span> <span } </span><span 18</span> ?>
主要是用到了key_exists函数与htmlspecialchars函数,关于这二个函数的用法,大家可以参考w3school手册上的介绍。