Jump to content


Photo

function wont allow white spaces


  • This topic is locked This topic is locked
6 replies to this topic

#1 Demonic

Demonic
  • Members
  • PipPipPip
  • Advanced Member
  • 562 posts

Posted 20 September 2006 - 02:44 AM

<?php
function therror($error){
print "<b>Sorry an error occured:</b>".$error;
return $error;
}
?>

Can anyone tell me why when i try and use this function it wont allow white spaces?

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 20 September 2006 - 03:14 AM

Can you tell us what you did, what you expected to happen and what actually happened?  We need all those 3 things to tell you what's going wrong.

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 September 2006 - 09:40 AM

By white spaces you mean line breaks/carriage returns

Eg you have a string like this:
This stirng
has 
line breaks
in it
But when you go to echo the string it displays like this:
This stirnghas line breaksin it

If that is whats happing its becuase the browser ignores whitespace chars. In order for the browser to parse whitespace characters you will need to force it, by either using the preformated text tag (<pre>text here</pre>). Or you'll have to convert the new lines into a HTML line break (<br />) using a PHP function called nl2br()

You're best of going with the PHP version. SO uses this as the PHP code:
<?php
function therror($error)
{
    print "<b>Sorry an error occured:</b>" . nl2br($error);

    return $error;
}
?>


#4 Demonic

Demonic
  • Members
  • PipPipPip
  • Advanced Member
  • 562 posts

Posted 20 September 2006 - 11:23 AM

Neither of theses

When i went to use the script it wouldnt allow to to use white spaces

therror(Hey);

would work^ when i tryed below it wouldnt work

therror(Hey that is not allowed);

O_O


Parse error: parse error, unexpected T_STRING in C:\thephp\xampp\htdocs\support\functions\errortest.php on line 8


code:
<?php
function therror($error)
{
    print "<b>Sorry an error occured:</b>" . nl2br($error);

    return $error;
}
therror(Hello person);
?>


#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 20 September 2006 - 11:34 AM

It would not allow white space in $error? Is that what you are saying?

The function call you have should pass the string as a string so put it in quotes like so...

theerror('Hello Person');
follow me on twitter @PHPsycho

#6 Demonic

Demonic
  • Members
  • PipPipPip
  • Advanced Member
  • 562 posts

Posted 20 September 2006 - 04:48 PM

Ah :) Thanks. ill see if that works.

#7 Demonic

Demonic
  • Members
  • PipPipPip
  • Advanced Member
  • 562 posts

Posted 20 September 2006 - 04:48 PM

Thanks works perfectly now XD.

closed




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users