php外部變數
Ⅰ php 裡面的變數怎麼用到外面的變數
$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
header('Content-type:text/plain');
foreach($posts as $post) {
switch ($post->post_type) {
case 'revision':
case 'nav_menu_item':
break;
case 'page':
$permalink = get_page_link($post->ID);
break;
case 'post':
$permalink = get_permalink($post->ID);
break;
case 'attachment':
$permalink = get_attachment_link($post->ID);
break;
default:
$permalink = get_post_permalink($post->ID);
break;
}
//echo ${permalink};
//echo "$posts";
//echo "\n{$permalink}";
//print_r($permalink);
//echo "\n{$post->post_type}\t{$permalink}\t{$post->post_title}";
//var_mp( $GLOBALS);
$url= ${permalink};
$urls=$url;
//echo "\n{$urls}";
}
echo "\n{$urls}";
$urls=substr($urls,0,-1);
$urls = explode(",",$urls);
Ⅱ Php 類中如何調用類外的變數
你好,有兩種方式可以解決問題。
1,將$m設置為超級全局變數..........****這種方法不推薦使用****
$GLOBALS['m']="mmmmmmm";//將m設置為超級全局變數
$a=$GLOBALS['m'];//在類的方法體中將m的值賦給a
2,將m的值作為參數傳給link 方法
function link($par){//設置形參
$a=$par;//賦值
}
link($m);//在調用link方法時,將$m傳給方法體
如有其它問題可Q22940449
Ⅲ PHP怎麼應用外部對象裡面的變數。
把你要做的事情寫成form1下的一個public的函數,用這個函數調用form1的變數和對象,然後在from2中調用這個函數。
Ⅳ php的class中怎樣引用外部的變數
用全局變數。
class MyClass {
public my_print() {
global $str;
print $str;
}
}
$str = 'Hello, World'.
$myclass = new MyClass();
$myclass->my_print();
Ⅳ PHP function 里 怎麼調用外部定義過的變數
很不習慣PHP中的變數作用域,PHP中函數變數和全局是完全隔絕的,也就是無法相互訪問。
比如下面這樣:
復制代碼代碼如下:
$test = 123;
abc(); //這里什麼都不輸出,因為訪問不到$test變數
function abc(){
echo($test);
}$test = 123;
abc(); //這里什麼都不輸出,因為訪問不到$test變數
function abc(){
echo($test);
}
如果,你想在函數內部訪問外部變數,你需要這樣:
復制代碼代碼如下:
$test = 123;
abc(); //輸出123
function abc(){
global $test;
echo($test);
}$test = 123;
abc(); //輸出123
function abc(){
global $test;
echo($test);
}
也就是說,你使用全局變數就可以在函數內調用外部定義過的變數
Ⅵ php的匿名函數怎麼訪問外部變數
一是訪問別的函數的返回結果
二是,直接變成全局變數 global
Ⅶ PHP閉包函數傳參及使用外部變數的方法
本文實例講述了PHP閉包函數傳參及使用外部變數的方法。分享給大家供大家參考,具體如下:
在Laravel控制器寫兩個方法,一個是在內部創建一個閉包函數,一個是執行傳過來的閉包函數,測試閉包的寫法,use使用外部變數,及閉包函數的傳參。如下:
//測試閉包傳參及use使用外部變數
public
function
testClosure($t1,
$t2)
{
$closure
=
function
($param1,
$param2)
use
($t1,
$t2)
{
echo
$param1.$param2.$t1.$t2;
};
$this->execClosure('test.closure',
$closure);
}
//執行閉包函數
protected
function
execClosure($name,
Closure
$closure)
{
echo
'Closure
func
name:'.$name;
echo
'<br>';
$closure('p1',
'p2');
}
在routes.php添加路由:
復制代碼
代碼如下:Route::get('/test/closure/{t1}/{t2}',['uses'=>'TestController@testClosure']);
訪問www.example.com/test/closure/hehe1/hehe2
瀏覽器輸出結果:
Closure
func
name:test.closure
p1p2hehe1hehe2
轉自:小談博客
http://www.tantengvip.com/2016/03/php-closure-use/
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。