Does php differentiate between 29 as a string and 29 as a number?

Although I know very little about computing, I have managed to make php send me a form, which is homework answers from students.
Every week, the number of answers is different, so I need to create different ‘thankyouWeekX.php’ files. I have automated this in Python. Below is part of the Python code:

filename = 'thankyou' + weeknr

print('How many boxes do we have? How many variables do we need?')

nrVariables = input()
# numVar = int(nrVariables)
PHP1 = ['\n','<?php\n', '$studentnr = $_POST[\'sn\'];\n']
for i in range(1, int(nrVariables) + 1):
	variable = basicPHPVariable.replace('X', str(i))
	PHP1.append(variable)

PHP1.append('\n')
PHP1.append('if($studentnr == \'\') {$studentnr = \'X\';}')
PHP1.append('\n')
PHP1.append('for ($i=1; $i <= ' + nrVariables + '; $i++) {\n')
PHP1.append('    if (${"q$i"} == \'\') ${"q$i"} = \'X\'; ')       
PHP1.append('}')
PHP1string = '\n'.join(PHP1)
file = open(pathToPHP + filename, 'a')
file.write(PHP1string)

In the middle, the php checks for empty answer boxes and writes an X if there is no answer at all. For example, the students often forget to enter their student number.

In this line:

PHP1.append('for ($i=1; $i <= ’ + nrVariables + ‘; $i++) {\n’)

nrVariables corresponds to the number of boxes to fill in. nrVariables is a string in Python. I can’t add an integer to a string in the line above.

Question: Will python read nrVariables as an integer, like 29 this week, in the for loop? Should I somehow declare nrVariables as an integer in php?

Why are you using two different languages to solve one problem?

I am not computer literate really. I can use Python to do very basic things and php only seems to run on web-hosts, as fas as I know. I can’t test my php file on my laptop. I have to upload it first.

I use Ubuntu 18 and I can use Python to generate basic PHP files and html files, like html tables, dropdown boxes, textboxes. I take the name=“X” in the html tag and replace X in a loop with whatever name I choose. Works great!

Same with a php file, use Python, make a list, then make a string: ’ '.join(phpList), then write it to a text file.

Php will run on your laptop. The easiest way to set up a local dev is install Laragon.

Using two languages the way you are is far from the way to handle things. Either use all Python or all Php, not a mix of both.

As far as I know, all interpreted language files are in fact just text files. That’s how I can use Python to make html files or php files. Nothing special.

Sponsor our Newsletter | Privacy Policy | Terms of Service