# How to increase the last element in this variable by +1?

``<?php \$p = "vopros1"; ?>` `
• \$ p = "vopros9"; What should be next? \$ p = "vopros0"; Then what is after him? \$ p = "vopros1"; Or after \$ p = "vopros9": \$ p = "vopros10"; Then it may be worth asking the question as follows: >> How to make a variable counter, in which the first part is a string, and the second is incremented? Then the answer will be something like this: \$ constPart = 'vopros'; \$ numPart = 1; \$ p = \$ constPart + \$ numPart; // \$ p = 'vopros1'; .... \$ numPart ++; \$ p = \$ constPart + \$ numPart; // \$ p = 'vopros2'; .... - BOPOH
• Or "vopros1" you get from somewhere? And you need to increase not the last character (read the number), but the last number? In general - is it possible to describe the problem in more detail, so that you can give a more or less adequate answer? - BOPOH

` `\$p = "vopros1"; echo substr_replace(\$p,substr(\$p,-1) + 1, -1); // vopros2` `
` `<?php const WHILE_COUNT = 10; \$count = NULL; \$p = 'vopros'; while (\$count<=WHILE_COUNT) { ++\$count; printf ("%s%s <br>\n",\$p,\$count); } ?>` `
` `\$p = "vopros1"; \$p = preg_replace_callback('/\d+/', function(\$m) { return ++\$m; }, \$p);` `

PHP 5.3+

<? php const WHILE_COUNT = 10; \$ count = NULL;

\$ p = 'vopros';

while (\$ count <= WHILE_COUNT) {++ \$ count; \$ p = 'vopros'. \$ count;
echo \$ p; }?>

It's so much easier for mine ... :) Or do you want to increase the variable by redirecting the page, then you need either a cookie or a session ....