php concatenation is removing php output? - php

I have a php variable that is created like so:
$scholarshipTitleOutput = ($scholarship['field_sec1_title']['#items'][0]['value']);
If I print the variable by itself it renders correctly, but I want the output to be placed between two header tags. What I would expect to be able to do it echo out the following...
echo "<h2>" . $scholarshipTitleOutput . "</h2>";
But this removes the variable output from the page for some reason. I've tested further and found that I can concatenate after echoing the variable to do things like add a space...
echo $scholarshipTitleOutput . " ";
So what is it about that first example that is completely removing the variable from outputting on the page?

It works fine when I try it. You can also try
echo "<h2>{$scholarshipTitleOutput}</h2>";

Related

How do i fix this dynamic query parameter in PHP?

I'm trying to get a URL to have a dynamic parameter based on the value of a variable.
Here's what Im doing
echo "The file name is ".$value.''.$value.''."<br>";
However, when I click on the corresponding link I get the following URL in the browser
http://localhost/HelloWorld/filespecificpage.php?filename=<?php echo $value ?> rather than the actual value in $value. Any help on how I can fix this would be appreciated.
PHP Noob here, appreciate the patience.
You're already in PHP script mode, you don't need <?php. You just need to concatenate the variable, like you did earlier in the line. You should also use urlencode() when substituting a variable into a URL parameter.
echo "The file name is ".$value.''.$value.''."<br>";
If you break the PHP String/Echo dont use the doublequotes, it makes your live much harder as you need ist.
For your question
echo 'The file name is ' . $value . '<a href="filespecificpage.php?filename=' . urlencode($value) . '>' . $value . '</a><br>';
An expample why single and not doublequotes. If you write an Hyperlink in HTML you use
Link description
and all is fine.
If you do it in an PHP echo with doublequotes you must escape all quotes.
echo "Link description";
Perfect look and way
echo '<a href="website" target="_blank" ' . $anyPHPvar . '>Link description</a>';
and you can use " for clear html and ' for PHP ;)
So, use singlequotes makes your life easyer and its a little bit faster and welcome to PHP ;)

How to concatenate variable with anchor's href

I'm noob in php, and i'm trying to concatenate href with words or values ex:
$name = "anonymous";
echo ''.$name.'<br/>';
I want it to look like this when the user clicked the link.
echo ''.$name.'<br/>';
I've tried doing this, but it's not working.
echo ''.$name.'<br/>';
echo ''.$name.'<br/>';
The same way you concatenated the text.
You cant use php tags inside a php tag like this,
echo ''.$name.'<br/>';
It will mess the PHP interpreter so use this
echo "<a href=profile.php/{$name}>".$name."</a><br/>";

Printing contents of array on separate lines

Experimenting with arrays and wondering why the following DOESN'T seem to print the values on SEPARATE lines when I run it?
<?php
$my_array = array("stuff1", "stuff2", "stuff3");
echo $my_array[0] . "\n";
echo $my_array[1] . "\n";
echo $my_array[2] . "\n";
?>
This makes the trick.
<?php
$my_array = array("stuff1", "stuff2", "stuff3");
foreach ( $my_array as $item ) {
echo $item . "<br/>";
}
?>
If your viewing the output in a web browser, newlines aren't represented visually. Instead you can use HTML breaks:
<?php
$my_array = array("stuff1", "stuff2", "stuff3");
echo implode('<br>', $my_array);
?>
From my PHP textbook:
One mistake often made by new php programmers (especially those from a
C background) is to try to break lines of text in their browsers by
putting end-of-line characters (“\n”) in the strings they print. To
understand why this doesn’t work, you have to distinguish the output
of php (which is usually HTML code, ready to be sent over the
Internet to a browser program) from the way that output is rendered
by the user’s browser. Most browser programs will make their own
choices about how to split up lines in HTML text, unless you force a
line break with the <BR> tag. End-of-line characters in strings will
put line breaks in the HTML source that php sends to your user’s
browser (which can still be useful for creating readable HTML
source), but they will usually have no effect on the way that text
looks in a Web page.
The <br> tag is interpreted correctly by all browsers, whereas the \n will generally only affect the source code and make it more readable.
You need to print with <br/> instead of \n because the default PHP mime type is HTML, and you use <br/> to accomplish line breaks in HTML.
For example,
<?php
$my_array = array("stuff1", "stuff2", "stuff3");
echo $my_array[0] . "<br/>";
echo $my_array[1] . "<br/>";
echo $my_array[2] . "<br/>";
?>
That's because in HTML a line break is <br />, not "\n".

redirecting using urlencode problem

Am trying to pass the value hrough the URL but when i retrieve it it appears like this ' , urlencode(15.99),'
the value is correct but i have tried different things but still unable to just send the value without the urlencode or added syntax
the line of code am trying to send the value is
redirect_to("$the_file_is?subj=' . urlencode($the_price_is)' ");
This should do what you're looking for, assuming I understood it correctly:
redirect_to($the_file_is . '?subj=' . urlencode($the_price_is));
Surely just change to...
redirect_to($the_file_is."?subj=" . urlencode($the_price_is));
Almost there - just a little bit of quote confusion :)
redirect_to("$the_file_is?subj=" . urlencode($the_price_is));
It's worth noting that you can reference variables in double quotes so:
echo "hello $name"; will work, but echo "$actioned"; might need to be echo "{$action}ed"; echo $action.'ed'; depending.

Show an Image if a PHP argument proves true

I barely know how to use PHP and I can't seem to make my code show an image if a condition proves true. This is the code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
}
?>
I tried writing this thinking it would work and it didn't:
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
Also I tried a code I found which started with the function: header() but it caused a tremendously long error, which said something like header already defined.
Thanks
You have used 'double quotes' incorrectly in the echo statement.
Try the following:
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg' alt='Preview not available' />"
Regards,
Mahendra Liya.
You should var_dump($first) to know what it contains
check if the condition is really getting true
and also put single quote inside the double quote.
if ($first=="#"){
echo 'yes it is true';
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
}
close the img tag
The part of the query string starting with # (so-called "hash") is not being sent to the server. That is, if your page is called like myblog.com/foo?bar=baz#quux, you php script will only receive myblog.com/foo?bar=baz. You need javascript if you want to handle urls with hashes.

Categories