PHP and undefined variables strategy

前端 未结 12 1192
名媛妹妹
名媛妹妹 2021-01-17 09:49

I am a C++ programmer starting with PHP. I find that I lose most of the debugging time (and my selfesteem!) due to undefined variables. From what I know, the only way to dea

12条回答
  •  轮回少年
    2021-01-17 10:23

    Just watch not to do operations that requires the variable value when using it the first time, like the concatenate operator, .=.

    If you are a C++ programmer you must be used to declare all variables. Do something similar to this in PHP by zeroing variables or creating empty array if you want to use them.

    Pay attention to user input, and be sure you have registered globals off and check inputs from $_GET and $_POST by isset().

    You can also try to code classes against structural code, and have every variable created at the beginning of a class declaration with the correct privacy policy.

    You can also separate the application logic from the view, by preparing all variables that have to be outputted first, and when it goes to display it, you will be know which variables you prepared.

提交回复
热议问题