php extra line breaks showing up - php

I'm a total newbie with php. I'm returning some variables for an address and am checking to see if an address2 exists. If it's null, I want it to skip and not put an extra line in the formatting. But, it seems to be breaking the line anyway. I've experimented with break tags all over the place and not, but can't find where it's coming in.
echo "$comp_name<br>$comp_add1";
if (isset($comp_add2)) {
echo "<br>$comp_add2"; }
echo "<br>$comp_city, $comp_state $comp_zip<br><a href=\"http://$comp_url\" >$comp_url</a>";
Take a look at this page: http://projects.ekcetera.com/people.php ... click on ABC Company, then check A Simplified Life, you'll see the extra line break in A Simplified Life.
What am I missing?

Your example page shows a fatal error, but I assume you have to use empty here, because the variable might be set, but only filled with an empty string.
So
if (isset($comp_add2)) {
echo "<br>$comp_add2";
}
should be
if (!empty($comp_add2)) {
echo "<br>$comp_add2";
}
Here is a good explanation regarding isset vs empty:
http://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

in A Simplified Life your $comp_add2 is set , you must check is it null or not
echo "$comp_name<br>$comp_add1";
if (!empty($comp_add2)) {
echo "<br>$comp_add2"; }
echo "<br>$comp_city, $comp_state $comp_zip<br><a href=\"http://$comp_url\" >$comp_url</a>";

Related

PHP returning extra line returns

When posting from JS, PHP (5.3.8) returns some line returns in excess. For example:
if (isset($_POST['postId']) && $_POST['postId'] == "test") {
echo 'ok';
}
returns ok↵↵↵
Is this by design? How can I get rid of those line returns?
No, this is not by design. You have three newline output.
Try to delete the close tag of php ?>, if you don't have any html after php code.
Another way is just put exit; right after echo, you won't get newline again.
... right below your
echo 'ok';
paste another snippet:
echo '--------new line------';
if the gap (extra returns) show between your first echo and the -----new line----- it would be more than strange:) However if it shows below the ------new line----- you have probably some extra HTML text (tags) below your PHP statement.

Sending information through URL using $_GET not working

So I'm trying to do something extremely simple, and after reading through forums, and researching on google I still can't figure out why this is not working. But this is mostly like because I'm still a very much noobie programmer. I'm trying to send information through a url, and having a script pick it up using the $_GET super global.
Here's the link code, in a file called TESTFORM.php:
<p>
Here's a link:
ID
</p>
This is the TESTGET.php script:
<?php
if (isset($_GET['id']))
echo 'it is set<br />';
else
echo 'it is not set<br />';
?>
This yields in a "It is not set" appearing on the page every time. Any thoughts? Are there ghosts in my computer ruining my code? Thanks for taking the time to read through this! Happy coding!
I'm no PHP programmer, but I do know from HTML that computers (especially file names) don't "like" spaces. Try removing the spaces in the id = 5 code.
Your problem is the extraneous space here around the URL parameters:
ID
That will result in PHP seeing the parameter as $_GET["id_"]. The space gets converted into an underscore.
It's always best to use var_dump($_GET); or var_dump($_REQUEST) when you run into such problems. Secondarily it is sometimes helpful to get rid of isset in such cases. Albeit you have a custom error message in place of the language notices intended just for that.
Have you tried to remove spaces in your link?
ID
Code seems fine at a glance, have you tried removing the spaces in
?id = 5 to ?id=5

php replacing string error

I am trying to delete a string from a string, but the result of strstr is not finding the string. I will try to be as clear as I can here....
The problem is strpos() is not finding $deletTabHTML. I have alerted it in ajax and it is exactly the same as a line in the commonHTML, but obviously it isn't for some reason I cannot figure out. I am assuming I am missing something 'invisible'? My script works if I hardcode the html to be deleted, so the overall script works.
here is the php:
$commonHTML = file_get_contents($url);
if (!empty($_POST['action']) && $_POST['action'] == 'deleteTab') {
$deletTabHTML = trim($_POST['theHTM']);
if(strpos($commonHTML, $deletTabHTML) !== false) {
$is_deleted="deleted";
}else{
$is_deleted="NOT deleted, ERROR:".$deletTabHTML;
}
echo '{"is_deleted":"' . $is_deleted . '"}';
return;
}
MORE INFO:
jQuery is getting an element from the dom and sending it to a php script which is opening a file and deleting the element:
<li id="contact">Contact</li>
The data returned to ajax is:
<li id="contact">Contact</li>
but for some reason it is not finding it. Thos were copied and pasted from the actual file and a javascript alert. They look exactly the same.
I hope that is enough info.
strpos is case sensitive
Try
stripos()
Aside from attempting to make the text search case insensitive, you also might want to make sure that it contains no unicode characters by using utf8_decode() on it first.
Lastly, it couldn't hurt to do some sanity checks on $_POST['theHTM'] before attempting to use it. (It looks like it's missing an L at the end, but it's also worth using isset() to verify that it actually exists.)

Invisible spaces in PHP function result

I'm working on making one of my first wordpress themes, but I seem to be encountering a weird issue. Whenever I call one of my functions with PHP, the return (when viewing the page) has a lot of white space (invisible characters). For some of the things I'm trying to do, it causes problems. Here's an example of one of my functions, the rest are built just like it.
// Get YouTube Username
function soc_youtube() {
global $up_options;
?>
<?php if($up_options->soc_youtube){ ?>
<?php echo $up_options->soc_youtube; ?>
<?php
}
}
That code generated this result:
Update: Fixed
Solution: Use less tags and cut down on breaks in code
Everything outside the php tags is pushed through directly to the output, including all your line breaks. To avoid that, leave the line breaks inside the PHP code:
<?php if($up_options->soc_youtube){
?><?php
echo $up_options->soc_youtube;
?><?php //...
(In your example, I don't see the need to close any of the tags at all, though. You could just have everything inside one set of tags.)
First, there is no need to end a php block and start it right back up again with nothing in between... especially on every single line. Try getting rid of those first and see if that makes a difference:
// Get YouTube Username
function soc_youtube() {
global $up_options;
if($up_options->soc_youtube){
echo $up_options->soc_youtube;
}
}
Next, if that doesn't work, try doing var_dump($up_options->soc_youtube); and see what's there and figure out why.

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