Assigning multiple variables to a single in PHP

家住魔仙堡 提交于 2019-12-11 08:12:10

问题


What I want to do is call all of my meta tags from one variable kept in the variables.php file.

I can echo them individually but I want to assign them all the same name so the echo script in the webpage is smaller. I understand that this is how you assign the same value to an integer but I cannot find the solution to do this with string values(I think they are string right?)

$john = $ jane = 3;

What I am trying.

$metaAuthor = $metaDescription = $metaImage = $metaTitle =$meta;

I assume its a different command when you are not using numbers?


回答1:


It is the same for every datatype, strings, ints, arrays, etc work all the same.

A little demo here:

http://sandbox.onlinephpfunctions.com/code/325104e0194b11b98fd7df58953aec4b0deb1468

Af of your comment you probably want this:

$meta = $metaAuthor." ".$metaDescription." ".$metaImage ." ".$metaTitle;

An array works like this:

$meta = array("author" => $metaAuthor, "description" => $metaDescription );
echo $meta['author']; 



回答2:


No it is the same with every value type.

http://sandbox.onlinephpfunctions.com/code/9211ff6bebf264fa106c28a85d789b8fb6b42c99

<?php
$meta = 'TEST';
$metaAuthor = $metaDescription = $metaImage = $metaTitle = $meta;
echo $metaAuthor."\n"; 
echo $metaDescription."\n";
echo $metaImage."\n";
echo $metaTitle."\n";
echo $meta."\n";
// all variables have now become equal to $meta



回答3:


Your code SHOULD assign whatever value is in $meta to all the other variables. In PHP, the "return value" or result of an assignment is the value being assigned. As long as $meta contains something, that something should be assigned everywhere else as well.

e.g.

php > $x = $y = $z = 7;
php > var_dump($x, $y, $z);
int(7)
int(7)
int(7)
php > $a = $b = $c = 'hello';
php > var_dump($a,$b,$c);
string(5) "hello"
string(5) "hello"
string(5) "hello"
php >


来源:https://stackoverflow.com/questions/15163596/assigning-multiple-variables-to-a-single-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!