Farid Qruzin Farid Qruzin - 3 months ago 7
PHP Question

My php function doesn't work - integer to string conversion issues

This is my php function it must return "5" but it returns nothing.

<?php
function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}

$numbers=456789;
echo get_second($numbers);
?>


When I tried out this code, this returns nothing (NULL, empty).
But I tried out this function below, worked perfectly.

<?php

function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}

$numbers=$_POST['number_input'];//that includes numbers
echo get_second($numbers);
?>


This code returns me second number of the post data. What I must do to work my First function? What is the difference between first $numbers variable and second $numbers variable?

Answer

Try this: (add quotes to your integer variable)

<?php
function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}

$numbers="456789";
echo get_second($numbers);
?>
Comments