user1149499
user1149499

Reputation: 583

Using SUBSTR to get two halves of a string

I'm hoping someone out there can see what I'm missing in using PHP's "substr" to get two halves of a 40 character string:

What I have is variable $dk that's 40 chars long. I've verified the variable's there with an echo statement and it's returning the right value. I've verified the "gkgk" = 1 or 2 as well. But I'm getting nothing as a result. Why?

$dk = "1234567891123456789212345678931234567894";
echo "<br><br>DK = ".$dk."<br><br>";
if ($a == 1) {
echo "A = 1<br><br>";
$gk = substr($dk, 0, 20); //I'm expecting the first 20 chars...12345678911234567892
} else {
echo "A = 2<br><br>";
$gk = substr($dk, 20, 20); //expecting the last 20 chars...12345678931234567894
}
echo "GK = ".$gk;

I tried putting the variable in quotes, just in case it was a syntax issue. All I get is

DK =1234567891123456789212345678931234567894

A = 1

GK =

No clue what's going wrong. I hope I've given enough explanation and my code so people can understand! Thank you for any help.

Upvotes: 0

Views: 69

Answers (1)

Code is correct but if you are checking $a is one or two, first of all u must add value to $a variable.

above of this ---> if ($a == 1) { <-------- add this $a=1;
or above of this ---> if ($a == 1) { <-------- add this $a=2;

Upvotes: 0

Related Questions