When I try and echo this string it shows "0"
I tried it on my local server and on http://writecodeonline.com/php/ both times same thing happened. This has never happened to me before, what is it and how do I fix? Thanks in advance.
<?PHP
$info = '
<div id="gallery_option_###number###">
<a href="#galleryButton###number###" onclick="gallery_button_down(' + "'###number###'" + ')">
Burn Notice
</a>
<div id="info_option_###number###">
<!--
[title]title[|title]
[description]test[|description]
[image]url[|image]
-->
</div>
</div>';
echo $info;
?>
You are following JavaScript way of string concatenation.
Read:
http://php.net/manual/en/language.operators.string.php
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>
Related
I'm trying to get strpos working the way I want, but stumbled over a few problems here.
The deal is I want to add a text to the title, if the title includes a specific work.
My title is represented as:
<h4><?php echo $article->name; ?></h4>
Then I want to use a code like this:
$a = '$article->name';
if (strpos($a, 'banana') !== false) {
echo 'is good';
}
However it it fails.
Does anyone know how I can get $a to read my title (which is a gode, not just a text)?
How do I replace the echo is good with a picture? (same issue here as my problem is that I dont understand how to get the code working withi the ''). I know the img src="", but just don't how to get it work in this code.
$a = $article->name;
if (strpos($a, 'banana') !== false) {
echo 'is good';
}
It seems that you are having trouble with variable interpolation in strings. Always good to check what is in the manual.
Basically, if you use single quotes the characters will be taken literally. So, to interpret a variable value inside a string, use double quotes.
Additionally, there is obviously no need to interpolate a single value:
$a = "$article->name"; # not needed
$a = $article->name; # better
And you can also concatenate values instead of interpolating them:
echo "<img src=\"$url\" />";
echo '<img src="' . $url . '" />';
$a = $article->name;
if (strpos($a, 'banana') !== false) {
echo '<img src="'.$article->img.'"/>' ;
}
Im assuming you have the adress of the image on the $article->img
And as others said you have to remove the single quotes in the first line.
Test like that:
<h4>
<?php $a = $article->name; if (strpos($a, 'elstock') !== false) {
$a .= 'is good';
}?>
<?php echo $a; ?>
</h4>
I think get string value from the variable is problems.
$a = '$article->name';
Test like that:
<h4>
<?php
$a = $article->name;
if((strpos($a, "banana")) !== false){
echo $a."is good";
}
?>
</h4>
This was complcated, I had to define the article as $title before I got this working...
<?php $a = strtolower($title." ".$description['title']); if (strpos($a, 'bana') !== false) { echo ' is good'; } ?>
string converting to array
<?php
$value1 = "Hello, world";
$convert = explode(" ",$value1);
echo $convert[1]; //Hello
echo $convert[2]; //world
// i convert particular number of array to string
$s = implode(" ", $convert[2]);
?>
I want output : world but I get this error message:
Error:
Warning: implode(): Invalid arguments passed in
I tried, but not done any one help
If you only want the output world, then this should work:
<?php
$value1 = "Hello, world";
$convert = explode(", ",$value1);
//echo $convert[0]; //Hello
echo $convert[1]; //world
?>
A begginers question. I have this little code:
<?php
$content = 'hello there, hello!';
echo substr_count("$content","hello");
?>
How could i replace 'hello there, hello!' part with another php enclosure, like
<?php the_content(); ?>
for example. What is the correct way of doing this? Thanks!
<?php
echo substr_count(the_content(),"hello");
?>
This:
<?php
$a = 10;
?>
<?php
$b = 20;
?>
<?php
echo $a + $b;
?>
--output:--
30
is equivalent to:
<?php
$a = 10;
$b = 20;
echo $a + $b;
?>
If the_content does not return a meaningful value but rather echos things out, use output buffering:
ob_start();
the_content();
$captured_content = ob_get_clean();
echo substr_count($captured_content, "hello");
$content = the_content();
echo substr_count($content,"hello");
or
echo substr_count(the_content(),"hello");
It is really the basics, try to look at some tutorials ;)
In PHP I can say
$noun = "bird";
$adjective = "warm;
echo <<<EOT
The $noun is very $adjective
EOT;
and it will output
The bird is very warm
Is there a way to do this with functions?
function getNoun() { return "bird"; }
function getAdjective() { return "warm"; }
echo <<<EOT
The getNoun() is very getAdjective()
EOT;
and output
The bird is very warm
You can use variable functions, though they're rather frowned on upon as they're not far different from variable variables...
$a = 'getNoun';
$b = 'getAdjective';
echo <<<EOT
The {$a()} is very {$b()}
EOT;
You could store it in a variable before using it:
$noun = getNoun( );
$adj = getAdjective( );
echo <<<EOT
The {$noun} is very {$adj}
EOT;
echo "The ";
echo getNoun();
echo " is very ";
echo getVerb();
Or maybe even (not 100% sure):
echo "The " . getNoun() . " is very " . getVerb();
Here is a solution using variable functions, although not exactly:
$noun=function($type){
return $type;
};
$adjective=function($adj){
return $adj;
};
echo "{$noun("Robin")} was very {$adjective("Warm")}";
how do I do when I want to preg_replace a href, but only if it's my own?
$a = 'href="http://mysite.com/?s=Bananas&lang=en"';
$host = 'http://mysite.com';
$a = preg_replace('#href="'.$host.'\/?[(s|p)]=([.*?])&lang=([.*?])"#e','href="index.php#$1\/$2\\lang\/$3"',$a);
//The result I want:
echo $a;
//Becomes href="http://mysite.com/#s/Bananas\\lang/en"
But what am I doing wrong?
This regex-syntax is very difficult...
<?php
$a = 'href="http://mysite.com/?s=Bananas&lang=en"';
$host = 'http://mysite.com';
echo preg_replace('#href="'.preg_quote($host).'/\?(s|p)=(.*?)&lang=(.*?)"#','href="'.$host.'/#$1/$2\\\\\lang/$3"',$a);
?>
This seems to work for me :)