Tags
PHP , concat
Asked 7 years ago
9 Mar 2017
Views 1147
lain

lain posted

PHP : concat problem


for($i=0;$i<=$page;$i++){ 
	if(!isset($_REQUEST['query'])){
			$page_Link="?query=".$i+1;
	}else {
			$page_Link="/query/".$i+1;
      }
 echo $page_Link;
}


echo $page_link should be ?page=1 or /page/1 but it retrun only 1

why concatenating in PHP not working well ?
Phpworker

Phpworker
answered Nov 30 '-1 00:00

Solution is

$page_Link="/page/".($i+1);


() make separate run of arithmetical operation and concatenation.


its problem type conversation .

if you do

$d="r"+1;


it return 1

why it is return 1 in auto type conversation with integer?

here + sign make it mathematical operation instead of concat


"r"+1 = intval("r")+1


intval("r") or intval("anystring") = 0 so 0+1=1

so solution is make separate run for $i+1 and concatenation .
so surround () it with so it run first and than it merged with string with (.) operation in PHP

Post Answer