tags:

views:

45

answers:

3

Hi! when the row['error'] is bigger than 35, the value isn't present and the result of the function is 0. can you pls tell me where is the problem?

<?php

if($row['error'] == "")
{
    $error="0";
}
else
{
    $error=$row['error'];
}

if($row['error'] != "")
{
   if(strlen($error) > 35)
       {
       $error = substr($row['error'],0,32) + "...";
       }
       else
       {
       $error = $row['error'];
       }
}  ?>
+5  A: 

Change

$error = substr($row['error'],0,32) + "...";

to:

$error = substr($row['error'],0,32) . "...";

The concatenate operator in PHP isn't a plus (+) sign; it's a period (.) sign

Kranu
thank you so much!
Ronny
No problem :) I made that mistake so many times when I started PHP.
Kranu
And now I'm making the opposite mistake in other languages! Great, short answer, Kranu.
Josh Smith
A: 

Because you check:

if(strlen($error) > 35) {
}
Alexander.Plutov
+3  A: 

Why all this code? Second condition is redundant, it doubles else condition from above
why not to make it all with just these few lines?

<?php
$error = $row['error'];
if(strlen($error) > 35) {
  $error = substr($row['error'],0,32) . "...";
}
?>
Col. Shrapnel
+1 for optimizing control flow, one of the biggest sources of bugs for beginners
Dennis Haarbrink
but i have to check if `$error == ""`..
Ronny
@Ronny What for? I see no point in it
Col. Shrapnel
because in this case, something different is happen. but of course, thank you!
Ronny
@Ronny In this code - no, nothing happen. Why don't you explain what you *think* will happen? I don't need your "thank you" nor asking for it. But I can correct my code for that case. Why don't you want to learn something useful?
Col. Shrapnel