Home > Backend Development > PHP Tutorial > php拼字符串 请教

php拼字符串 请教

WBOY
Release: 2016-06-23 13:39:41
Original
1043 people have browsed it

每次拼字符串,感觉头大。 因为没有这坏习惯


在改一段老代码 
引用一个js方法,如下

删除


现在要把上边的 删除写法,放到下边的 删除上边,可不知道怎么拼
 echo " $intserial ".$value['realname']." ".$value['mobile']." ".$arrgender[$value['gender']]." ".$value['idnumber']." ".$value['passportnu']." 删除 ";

--------------------------------------------------------------------------------------------------------------
写了几个都不对
错误一:
  echo " $intserial ".$value['realname']." ".$value['mobile']." ".$arrgender[$value['gender']]." ".$value['idnumber']." ".$value['passportnu']." 删除 ";


回复讨论(解决方案)

你最终需要的 HTML 是什么样的?
由此出发,才能把事情做好

你的 href="javascript:confirmDelete('delgroup.asp?gid=14','组一旦删除,数据将无法恢复。您确实要删除此记录吗?');"
含义不明

1.  双引号和单引号不能嵌套,如果嵌套,中间的部分要加转义。
2.  单引号和双引号可以交替使用。
3. 引号较多的,可以使用heredoc语法
4. 拼接字符串较多时,你可以一次只拼接一行。
$str .= '

';
$str .= "";

基于这些原则,拼接字符串也是很容易的:
拼接1:
<?php    function base_url(){        return "http://base/";    }    $value = array(        'userid' => 1,        'realname' => 'name',        'mobile' => '13590909090',        'gender' => '1',        'idnumber' => 'idnum',        'passportnu' => 'passport',    );    $intserial = 'ang?';    echo "<tr>        <td>$intserial</td>        <td><a href='" . base_url() . "team/alterinfo?id=" . $value['userid'] . "'>" . $value['realname']."</a></td>        <td>" . $value['mobile'] . "</td><td>" . $value['gender'] . "</td>        <td>" . $value['idnumber'] . " " . $value['passportnu'] . "</td>        <td><a href='javascript:confirmDelete(\"" . base_url() . "team/deletemember?id=" . $value['userid'] . "\",'confirm delet    e?')'>delete</a></td>        </tr>";
Copy after login


拼接2 :
$str  = "<tr>";$str .= "<td>$intserial</td>";$str .= "<td><a href='" . base_url() . "team/alterinfo?id=" . $value['userid'] . "'>" . $value['realname']."</a></td>";$str .= "<td>" . $value['mobile'] . "</td><td>" . $value['gender'] . "</td>";$str .= "<td>" . $value['idnumber'] . " " . $value['passportnu'] . "</td>";$str .=  "<td><a href='javascript:confirmDelete(\"" . base_url() . "team/deletemember?id=" . $value['userid'] . "\",\"confir    m delete?\")'>delete</a></td>";$str .= "</tr>";echo $str;
Copy after login


第二种方法每次只拼接一小部分, 拼接出错的概率更低。

如果你是html与php混写,那么完全可以只用php echo 变量,其余的部分用html代码。

多谢两位前辈

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template