PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 22, 2022

[FIXED] How to assign a string-value to a variable with exec()?

 July 22, 2022     exec, python, string     No comments   

Issue

I know there are plenty of these questions already, but I still couldn't figure out a solution for my problem:

I am trying to assign an unknown string value (like "M10" or something similar) to an unknown variable name by using exec. Therefore I tried the following, but it does not work:

name  = "test"
value = "m10"
exec("%s=%s" % (name, value), globals())
exec("{}={}".format(name, value), globals())
print(test)

It throws this error: NameError: name 'm10' is not defined.

As soon as I change the value to 10 for example, it works totally fine.

What am I missing here?


Solution

quotes around m10, it thinks you're trying to assign a variable to test

value = "'m10'"


Answered By - Sayse
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing