0

i want to understand why name and value of input has '%' , isearched alot and i didn't find what it means : '

full code :

<body>
<img src="1.jpg" alt="banner" style="width:1000px;height:300px;">
<div style="margin:0 auto;width:75%;text-align:center;">
<form name = "ticktactoe" method = "post" action = "index.php">
    <?php
        for($i = 0; $i <=8; $i++)
        {
            printf('<input type = "text" id = "ip" name = "box%s" value = "%s">', $i, $box[$i]);
            if ($i == 2 || $i == 5 || $i == 8){
            print("<br>");
            }
        }
        if($winner == 'n')
        {
            print('<input type = "submit" name = "gobtn" value = "Next Move" id = "go">');
        }
        else
        {
            print('<input type = "button" name = "newgamebtn" value = "Play Again" id = "go" onclick = "window.location.href=\'index.php\'">');
        }

    ?>
</form>
</div>
---
Reem
  • 1

2 Answers2

0

These characters "%" are used in PHP in printf function to format the text with the values that are passed after the string.

The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result and conversion specifications, each of which results in fetching its own parameter.

A conversion specification follows this prototype: %[argnum$][flags][width][.precision]specifier.

Filipizaum
  • 21
  • 4
  • can i know why we set blank to 0 then why we make condition although blank must take the last for result --- $blank = 0; for ($i = 0; $i <= 8 ; $i++) { if($box[$i] == '') { $blank = 1; } } if($blank == 1) { $i = rand() % 8; //$i in range between 0 to 8 while($box[$i] != '') { $i = rand() % 8; } – Reem Mar 10 '21 at 22:20
  • In that code, if any element of `$box` is an empty string (`''`) `$blank` will be set to `1`, thus you will get in the condition ( `if($blank == 1)` ). But the code inside that condition is apparently wrong, there is an `$i` variable receiving some random value and being used as index to check a value in the array in a while loop. If the value found is a empty string, the while loop stop iterating, and I think it is not the purpose. – Filipizaum Mar 10 '21 at 23:19
0

This question has its whole wikipedia article : https://en.wikipedia.org/wiki/Printf_format_string

What you are talking about is called "format string".

Basically there are placeholders, like in your case %s for strings, but also %f for floats, %i or %d for integers, etc.

There is a simple example :

printf("Your age is %d", age);

See, the variable "age" here is obviously an integer but we can't just concatenate an integer to a string, hence the format strings.

Loïc
  • 10,366
  • 1
  • 26
  • 42