English 中文(简体)
基本 PHP: 在命令中使用变量
原标题:Basic PHP: Using Variables In Commands
  • 时间:2012-05-23 19:45:26
  •  标签:
  • php

我对 PHP 相当新, 我在 WordPress 中工作, 但我不认为这个问题是 WordPress 所指定的 。 基本上, 我运行了一个 MySQL 查询, 返回一些数字 。 在 WordPress 中, 您可以使用一个名为 wp_ list_ pages () 的函数来表示您想要包含的页面, 比如 :

 wp_list_pages( include=161,164,167,171,172,173,174,185,188,135,141,&title_li= );

三位数数字都是后代身份识别码。我想做的是用程序生成这些数字,而不是硬码。所以,我使用以下方法:

    $results = $wpdb->get_results("SELECT * FROM [table] WHERE [column] LIKE [string] ");
foreach($results as $returned_id) {$pagelist.=$returned_id->post_id.",";}

这工作很好( 括号中的所有内容都替换为实际信息 ) 。 它基本上是在符合我的 SQL 查询的 $ pagelist 变量中创建一个列表。 如果我运行“ echo $results ”, 输出将是“ 161, 164, 167... ”, 等, 也就是硬编码版本的精确列表 。

现在,我想用程序版本取代硬编码版本----我想应该是类似

wp_list_pages( include=$pagelist );

但是这行不通。 我想用变量的内容来取代那个例子中的$page list 。 我该怎么做呢?

最佳回答

<强 > 备选1

如果您想要评估变量,需要使用双引号。

wp_list_pages("include=$pagelist")

wp_list_pages("include={$pagelist}")

<强 > 备选2

使用字符串连接

wp_list_pages( include=  . $pagelist);

<强 > 备选3

使用 < a href=> "http://php.net/manual/en/formation.sprintf.php" rel="nofollow"\\\code>sprintf

wp_list_pages(sprintf( include=%s ,$pagelist));
问题回答

在 PHP 中, PHP 变量仅以双引号查找, 而不是您的位置值。 所以使用 " 而不是 "

wp_list_pages("include=$pagelist");

您可以做的另一件事是将变量与字符串的字符串混为一变数, 像这样 :

wp_list_pages( include= .$pagelist);

你有没有试过:

wp_list_pages( include=  . $pagelist);

wp_list_pages("include={$pagelist}");

对于字符串中的参考变量,您需要使用双引号。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签