I've been looking everywhere about text formatting with echo but I couldn't find a simple answer on how do I echo my variables like this: field1(field2,field3,field4): field5. Without using $row['field'], or maybe it would be even easier that way?
Thanks!
Your question is not very clear. Are you trying to echo an array ?
If so check the print_r function (http://www.php.net/print_r).
print_r($your_array_here)
Or you are trying to join the variables ? (http://www.php.net/implode)
echo implode(',', array($variable1, $variable2, $variable3));
I dont think there is a way to do this (if you are really talking about data right from the DB) as you will always have an array and you have to access that array. Post what is it you are trying to achieve, perhaps there is a faster way.
Related
URL: http://example.com/?var=foo&var=bar
How to make PHP set $_GET['var'] as "foo" and not "bar", without parsing manually $_SERVER['QUERY_STRING']? Maybe there is some option in the php.ini file?
Unfortunately in PHP you cannot do this. You should use an array:
http://example.com/?var[]=foo&var[]=bar
You will get the following in PHP:
$_GET['var'] = ['foo', 'bar']
You will find this is different in other languages such a golang where your first example will actually get the same result.
You can find a brilliant answer on this answer.
No built in way to do this. PHP's $_GET wasn't designed for this practice, for it is an uncommon one. I recommend against it.
terrible idea, but i was bored:
$url="http://example.com/?var=foo&var=bar";
$x=parse_url($url, PHP_URL_QUERY); //get the query string
$x=explode('&',$x); //break on &
$x=explode('=',$x[0]); //break on =
echo $x[1]; // =foo
update link to
http://localhost/index.php?var[]=foo&var[]=bar
<?php
$foo=$_GET["var"][0];
echo $foo;//foo
print_r($_GET);//full array
?>
How to use a varible to store the value of the for loop?
for($i=1;$i<=$var;$i++)
How to get $i's value and store into another value and show its result?
I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!
Are you want dynamic variable or just a normal variable which will give you the out put like 12345678910
<?php
$var=10;
$a='';
for($i=1;$i<=$var;$i++){
$a.=$i;
}
echo $a;
?>
output: http://3v4l.org/2p8L6
or if you want dynamic variable then use following code to make a variable dynamic
${"num_" . $i} = $i;
output will be
$num_1=1;
$num_2=2;
$num_3=3;
etc
In plain English; PHP is updating the value stored in lopping variable automatically, and this is the whole idea behind using for loops, not only in PHP, but also in other programming languages.
Usually, you would want to use it (the variable $i) as a kind of a counter, and run code in the loop based on the iteration count.
so I want to show the result of my query, and I can't seem to find anything that works.
this is what I have so far and I have no idea why it won't work.
daydream festival <?php
$queryday="SELECT `tickets_MAX`-`tickets_VERKOCHT` AS `tickets_over` FOR `evenementdata` WHERE `ev id`=1";
$resultday=mysql_query($queryday);
$rowday = mysql_fetch_assoc($resultday);
echo $rowday;?> tickets left
Building on other reasons why this won't print anything like your rows not existing, or misnaming things.
If I'm not mistaken mysql_fetch_assoc returns an array, echo does not print arrays. You need to use print_r($rowday); - Assuming that $rowday contains data.
I'm making a few assumptions here, but I think it's likely that you should do it like:
<?php
$queryday="SELECT `tickets_MAX`-`tickets_VERKOCHT` AS `tickets_over` FROM `evenementdata` WHERE `ev_id`=1";
$resultday=mysql_query($queryday);
$rowday = mysql_fetch_assoc($resultday);
print_r($rowday);
?>
That being said, I would strongly recommend not using the mysql_ functions, since they are now being depreciated. You should do some research into The PHP Data Objects (PDO).
I am currently achieving the desired outcome with two PHP statements:
$thisBlarg = $xmlResource->xpath('//blarg[#ID='.$someBlargID.']');
echo $thisBlarg[0]->name;
But, not wanting to settle for second best, I'd really prefer this to be one statement, but PHP doesn't like this:
echo $xmlResource->xpath('//blarg[#ID='.$someBlargID.']')[0]->name;
And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?
Try this
echo current(($xmlResource->xpath('//blarg[#ID='.$someBlargID.']')))->name;
I have tried to do this:
<?=implode(array('A','B','C'));
To display an array but was wondering if there was an easier way of showing an array?
I tried
<?=print_r(array('A','B','C'));
But it actually displays an array structure. I want the string to be like ABC.
No, there is no easier way than this implode.
Normally while debugging array printing is done using implode() and print_r(). If you want to display the arrays in your different way, just create your own function for that.
I have seen in many examples var_dump(...).
link text
Yeah, sure... If you always want an array to display, say, with each element on a new line, you can just write your own function (with a really short name... if you are just that lazy... [not recommended...])!
<?php
// To Call Function:
$array = array(2,3,4,5,'awesome!');
ez($array);
/* echoes:
2
3
4
5
awesome!
*/
// Poorly named function...
function ez($array = array()) {
if(!$array || empty($array)) return;
$output = implode('\n',$array);
echo "<pre>{$output}</pre>";
}
?>
Sorry, this is kind of a smartass answer.