How to change a n value for a value like XHow can I prevent SQL injection in PHP?How to change envelope from...

How do I append a character to the end of every line in an Excel cell?

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

What is a good reason for every spaceship to carry a weapon on board?

Which one of these password policies are more secure?

How to deal with an incendiary email that was recalled

Why don't hotels mount air conditioning units outside the rooms?

What is the purpose of easy combat scenarios that don't need resource expenditure?

What is the wife of a henpecked husband called?

A curious equality of integrals involving the prime counting function?

Why are the books in the Game of Thrones citadel library shelved spine inwards?

False written accusations not made public - is there law to cover this?

Why avoid shared user accounts?

What are "industrial chops"?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

When can a QA tester start his job?

Why am I able to open Wireshark and capture packets in macOS without root privileges?

Am I a Rude Number?

What is the difference between rolling more dice versus fewer dice?

Making him into a bully (how to show mild violence)

Consequences of lack of rigour

Graph with overlapping labels

Salesforce package error error “You can't specify version for namespace sf_com_apps because this namespace is not installed to your organization.”

Why is Agricola named as such?

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?



How to change a n value for a value like X


How can I prevent SQL injection in PHP?How to change envelope from address using PHP mail?How do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How to Sort Multi-dimensional Array by Value?How Do You Parse and Process HTML/XML in PHP?How do I check if a string contains a specific word?How does PHP 'foreach' actually work?Email PHP Contact Form error as random outputHow to collect email in form (html) and send to external email using (PHP)?













7















I have a webpage for homework. Students fill in the boxes, then click send to send the form.
My php thankyou file gets the answers and sends to my email. I download the emails and use Python to check each answer against the correct answer, line for line using readlines().



This morning I noticed, one student missed the first part, 1 to 5 'choose the right word from the table.' They should just enter a letter, A to E, in each textbox, G1 to G5. The email sent by php then contains 5 empty lines, n, where the answers should be.



Because students often press enter in strange places, I run a Python script to get rid of empty lines in the emails after downloading.



So I would like to get php to put say X if a textbox is empty, before it sends the email.



The first part of the thankyou.php looks like this:



//should mail the contact form
<?php
$studentnr = $_POST['sn'];
$q1 = $_POST['G1'];
$q2 = $_POST['G2'];
$q3 = $_POST['G3'];
$q4 = $_POST['G4'];
$q5 = $_POST['G5'];


What I would like to do in php is something along the lines of:



for i in range(1, 6):  
answer = $q + str(i)
if answer = '':
answer = 'X'


but that would be Python. What is the right way to do this in php?



I think this should be done after collecting all the $qs in the php script, but before making the body of the email:



$body = "

Studentnr = ".$studentnr."
".$q1."
".$q2."
".$q3."
".$q4."
".$q5."
";


Very grateful for any tips!



Edit: this is an actual thankyou.php with the loop to change '' for X, but all I get now in the email is: Studentnr = 1725010999, nothing else. How to tweak this? I just entered the student number and left all other boxes empty, so I was expecting a lot of Xs. I am not getting any errors in the error log of my php directory on the webpage host. Maybe a ; missing somewhere?



//should mail the contact form
<?php
$studentnr = $_POST['sn'];
$q1 = $_POST['G1'];
$q2 = $_POST['G2'];
$q3 = $_POST['G3'];
$q4 = $_POST['G4'];
$q5 = $_POST['G5'];
$q6 = $_POST['G6'];
$q7 = $_POST['G7'];
$q8 = $_POST['G8'];
$q9 = $_POST['G9'];
$q10 = $_POST['G10'];
$q11 = $_POST['G11'];
$q12 = $_POST['G12'];
$q13 = $_POST['G13'];
$q14 = $_POST['G14'];
$q15 = $_POST['G15'];
$q16 = $_POST['G16'];
$q17 = $_POST['G17'];
$q18 = $_POST['G18'];

for ($i=1; $i <= 18; $i++) {
if (${"q$i"} == '') ${"q$i"} = 'X';
}

$body = "

Studentnr = ".$studentnr."
".$q1."
".$q2."
".$q3."
".$q4."
".$q5."
".$q6."
".$q7."
".$q8."
".$q9."
".$q10."
".$q11."
".$q12."
".$q13."
".$q14."
".$q15."
".$q16."
".$q17."
".$q18."
";


$to1 = "myemail@foxmail.com";

$subject = $studentnr . "sWeek1";
$headers = "From: peter@mypage.comrn";
$headers .= 'Content-Type: text/plain; charset=utf-8';

mail($to1, $subject, $body, $headers);

header("Location: email_success.php");

?>


Any more tips please?










share|improve this question





























    7















    I have a webpage for homework. Students fill in the boxes, then click send to send the form.
    My php thankyou file gets the answers and sends to my email. I download the emails and use Python to check each answer against the correct answer, line for line using readlines().



    This morning I noticed, one student missed the first part, 1 to 5 'choose the right word from the table.' They should just enter a letter, A to E, in each textbox, G1 to G5. The email sent by php then contains 5 empty lines, n, where the answers should be.



    Because students often press enter in strange places, I run a Python script to get rid of empty lines in the emails after downloading.



    So I would like to get php to put say X if a textbox is empty, before it sends the email.



    The first part of the thankyou.php looks like this:



    //should mail the contact form
    <?php
    $studentnr = $_POST['sn'];
    $q1 = $_POST['G1'];
    $q2 = $_POST['G2'];
    $q3 = $_POST['G3'];
    $q4 = $_POST['G4'];
    $q5 = $_POST['G5'];


    What I would like to do in php is something along the lines of:



    for i in range(1, 6):  
    answer = $q + str(i)
    if answer = '':
    answer = 'X'


    but that would be Python. What is the right way to do this in php?



    I think this should be done after collecting all the $qs in the php script, but before making the body of the email:



    $body = "

    Studentnr = ".$studentnr."
    ".$q1."
    ".$q2."
    ".$q3."
    ".$q4."
    ".$q5."
    ";


    Very grateful for any tips!



    Edit: this is an actual thankyou.php with the loop to change '' for X, but all I get now in the email is: Studentnr = 1725010999, nothing else. How to tweak this? I just entered the student number and left all other boxes empty, so I was expecting a lot of Xs. I am not getting any errors in the error log of my php directory on the webpage host. Maybe a ; missing somewhere?



    //should mail the contact form
    <?php
    $studentnr = $_POST['sn'];
    $q1 = $_POST['G1'];
    $q2 = $_POST['G2'];
    $q3 = $_POST['G3'];
    $q4 = $_POST['G4'];
    $q5 = $_POST['G5'];
    $q6 = $_POST['G6'];
    $q7 = $_POST['G7'];
    $q8 = $_POST['G8'];
    $q9 = $_POST['G9'];
    $q10 = $_POST['G10'];
    $q11 = $_POST['G11'];
    $q12 = $_POST['G12'];
    $q13 = $_POST['G13'];
    $q14 = $_POST['G14'];
    $q15 = $_POST['G15'];
    $q16 = $_POST['G16'];
    $q17 = $_POST['G17'];
    $q18 = $_POST['G18'];

    for ($i=1; $i <= 18; $i++) {
    if (${"q$i"} == '') ${"q$i"} = 'X';
    }

    $body = "

    Studentnr = ".$studentnr."
    ".$q1."
    ".$q2."
    ".$q3."
    ".$q4."
    ".$q5."
    ".$q6."
    ".$q7."
    ".$q8."
    ".$q9."
    ".$q10."
    ".$q11."
    ".$q12."
    ".$q13."
    ".$q14."
    ".$q15."
    ".$q16."
    ".$q17."
    ".$q18."
    ";


    $to1 = "myemail@foxmail.com";

    $subject = $studentnr . "sWeek1";
    $headers = "From: peter@mypage.comrn";
    $headers .= 'Content-Type: text/plain; charset=utf-8';

    mail($to1, $subject, $body, $headers);

    header("Location: email_success.php");

    ?>


    Any more tips please?










    share|improve this question



























      7












      7








      7








      I have a webpage for homework. Students fill in the boxes, then click send to send the form.
      My php thankyou file gets the answers and sends to my email. I download the emails and use Python to check each answer against the correct answer, line for line using readlines().



      This morning I noticed, one student missed the first part, 1 to 5 'choose the right word from the table.' They should just enter a letter, A to E, in each textbox, G1 to G5. The email sent by php then contains 5 empty lines, n, where the answers should be.



      Because students often press enter in strange places, I run a Python script to get rid of empty lines in the emails after downloading.



      So I would like to get php to put say X if a textbox is empty, before it sends the email.



      The first part of the thankyou.php looks like this:



      //should mail the contact form
      <?php
      $studentnr = $_POST['sn'];
      $q1 = $_POST['G1'];
      $q2 = $_POST['G2'];
      $q3 = $_POST['G3'];
      $q4 = $_POST['G4'];
      $q5 = $_POST['G5'];


      What I would like to do in php is something along the lines of:



      for i in range(1, 6):  
      answer = $q + str(i)
      if answer = '':
      answer = 'X'


      but that would be Python. What is the right way to do this in php?



      I think this should be done after collecting all the $qs in the php script, but before making the body of the email:



      $body = "

      Studentnr = ".$studentnr."
      ".$q1."
      ".$q2."
      ".$q3."
      ".$q4."
      ".$q5."
      ";


      Very grateful for any tips!



      Edit: this is an actual thankyou.php with the loop to change '' for X, but all I get now in the email is: Studentnr = 1725010999, nothing else. How to tweak this? I just entered the student number and left all other boxes empty, so I was expecting a lot of Xs. I am not getting any errors in the error log of my php directory on the webpage host. Maybe a ; missing somewhere?



      //should mail the contact form
      <?php
      $studentnr = $_POST['sn'];
      $q1 = $_POST['G1'];
      $q2 = $_POST['G2'];
      $q3 = $_POST['G3'];
      $q4 = $_POST['G4'];
      $q5 = $_POST['G5'];
      $q6 = $_POST['G6'];
      $q7 = $_POST['G7'];
      $q8 = $_POST['G8'];
      $q9 = $_POST['G9'];
      $q10 = $_POST['G10'];
      $q11 = $_POST['G11'];
      $q12 = $_POST['G12'];
      $q13 = $_POST['G13'];
      $q14 = $_POST['G14'];
      $q15 = $_POST['G15'];
      $q16 = $_POST['G16'];
      $q17 = $_POST['G17'];
      $q18 = $_POST['G18'];

      for ($i=1; $i <= 18; $i++) {
      if (${"q$i"} == '') ${"q$i"} = 'X';
      }

      $body = "

      Studentnr = ".$studentnr."
      ".$q1."
      ".$q2."
      ".$q3."
      ".$q4."
      ".$q5."
      ".$q6."
      ".$q7."
      ".$q8."
      ".$q9."
      ".$q10."
      ".$q11."
      ".$q12."
      ".$q13."
      ".$q14."
      ".$q15."
      ".$q16."
      ".$q17."
      ".$q18."
      ";


      $to1 = "myemail@foxmail.com";

      $subject = $studentnr . "sWeek1";
      $headers = "From: peter@mypage.comrn";
      $headers .= 'Content-Type: text/plain; charset=utf-8';

      mail($to1, $subject, $body, $headers);

      header("Location: email_success.php");

      ?>


      Any more tips please?










      share|improve this question
















      I have a webpage for homework. Students fill in the boxes, then click send to send the form.
      My php thankyou file gets the answers and sends to my email. I download the emails and use Python to check each answer against the correct answer, line for line using readlines().



      This morning I noticed, one student missed the first part, 1 to 5 'choose the right word from the table.' They should just enter a letter, A to E, in each textbox, G1 to G5. The email sent by php then contains 5 empty lines, n, where the answers should be.



      Because students often press enter in strange places, I run a Python script to get rid of empty lines in the emails after downloading.



      So I would like to get php to put say X if a textbox is empty, before it sends the email.



      The first part of the thankyou.php looks like this:



      //should mail the contact form
      <?php
      $studentnr = $_POST['sn'];
      $q1 = $_POST['G1'];
      $q2 = $_POST['G2'];
      $q3 = $_POST['G3'];
      $q4 = $_POST['G4'];
      $q5 = $_POST['G5'];


      What I would like to do in php is something along the lines of:



      for i in range(1, 6):  
      answer = $q + str(i)
      if answer = '':
      answer = 'X'


      but that would be Python. What is the right way to do this in php?



      I think this should be done after collecting all the $qs in the php script, but before making the body of the email:



      $body = "

      Studentnr = ".$studentnr."
      ".$q1."
      ".$q2."
      ".$q3."
      ".$q4."
      ".$q5."
      ";


      Very grateful for any tips!



      Edit: this is an actual thankyou.php with the loop to change '' for X, but all I get now in the email is: Studentnr = 1725010999, nothing else. How to tweak this? I just entered the student number and left all other boxes empty, so I was expecting a lot of Xs. I am not getting any errors in the error log of my php directory on the webpage host. Maybe a ; missing somewhere?



      //should mail the contact form
      <?php
      $studentnr = $_POST['sn'];
      $q1 = $_POST['G1'];
      $q2 = $_POST['G2'];
      $q3 = $_POST['G3'];
      $q4 = $_POST['G4'];
      $q5 = $_POST['G5'];
      $q6 = $_POST['G6'];
      $q7 = $_POST['G7'];
      $q8 = $_POST['G8'];
      $q9 = $_POST['G9'];
      $q10 = $_POST['G10'];
      $q11 = $_POST['G11'];
      $q12 = $_POST['G12'];
      $q13 = $_POST['G13'];
      $q14 = $_POST['G14'];
      $q15 = $_POST['G15'];
      $q16 = $_POST['G16'];
      $q17 = $_POST['G17'];
      $q18 = $_POST['G18'];

      for ($i=1; $i <= 18; $i++) {
      if (${"q$i"} == '') ${"q$i"} = 'X';
      }

      $body = "

      Studentnr = ".$studentnr."
      ".$q1."
      ".$q2."
      ".$q3."
      ".$q4."
      ".$q5."
      ".$q6."
      ".$q7."
      ".$q8."
      ".$q9."
      ".$q10."
      ".$q11."
      ".$q12."
      ".$q13."
      ".$q14."
      ".$q15."
      ".$q16."
      ".$q17."
      ".$q18."
      ";


      $to1 = "myemail@foxmail.com";

      $subject = $studentnr . "sWeek1";
      $headers = "From: peter@mypage.comrn";
      $headers .= 'Content-Type: text/plain; charset=utf-8';

      mail($to1, $subject, $body, $headers);

      header("Location: email_success.php");

      ?>


      Any more tips please?







      php






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      Pedroski

















      asked 4 hours ago









      PedroskiPedroski

      1725




      1725
























          2 Answers
          2






          active

          oldest

          votes


















          4














          You can do that like this:



             for($i=1; $i <= 5; $i++) {
          $answer= ${'q' . $i};
          if($answer=='') $answer='X';
          }


          Edit:
          If you just only need to update value, you can do that like this,



             for($i=1; $i <= 5; $i++) {
          if(${'q' . $i}=='') ${'q' . $i}='X';
          }





          share|improve this answer


























          • Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

            – Pedroski
            3 hours ago













          • Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

            – Whatatimetobealive
            2 hours ago



















          3














          If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:



          $q1 = '';
          $q2 = 'A';
          $q3 = '';
          $q4 = 'E';
          $q5 = 'D';
          for ($i=1; $i <= 5; $i++) {
          if (${"q$i"} == '') ${"q$i"} = 'X';
          }
          echo "$q1n";
          echo "$q2n";
          echo "$q3n";
          echo "$q4n";
          echo "$q5n";


          Output:



          X
          A
          X
          E
          D


          Demo on 3v4l.org






          share|improve this answer


























          • So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

            – Pedroski
            3 hours ago











          • @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

            – Nick
            3 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54917530%2fhow-to-change-a-n-value-for-a-value-like-x%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You can do that like this:



             for($i=1; $i <= 5; $i++) {
          $answer= ${'q' . $i};
          if($answer=='') $answer='X';
          }


          Edit:
          If you just only need to update value, you can do that like this,



             for($i=1; $i <= 5; $i++) {
          if(${'q' . $i}=='') ${'q' . $i}='X';
          }





          share|improve this answer


























          • Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

            – Pedroski
            3 hours ago













          • Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

            – Whatatimetobealive
            2 hours ago
















          4














          You can do that like this:



             for($i=1; $i <= 5; $i++) {
          $answer= ${'q' . $i};
          if($answer=='') $answer='X';
          }


          Edit:
          If you just only need to update value, you can do that like this,



             for($i=1; $i <= 5; $i++) {
          if(${'q' . $i}=='') ${'q' . $i}='X';
          }





          share|improve this answer


























          • Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

            – Pedroski
            3 hours ago













          • Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

            – Whatatimetobealive
            2 hours ago














          4












          4








          4







          You can do that like this:



             for($i=1; $i <= 5; $i++) {
          $answer= ${'q' . $i};
          if($answer=='') $answer='X';
          }


          Edit:
          If you just only need to update value, you can do that like this,



             for($i=1; $i <= 5; $i++) {
          if(${'q' . $i}=='') ${'q' . $i}='X';
          }





          share|improve this answer















          You can do that like this:



             for($i=1; $i <= 5; $i++) {
          $answer= ${'q' . $i};
          if($answer=='') $answer='X';
          }


          Edit:
          If you just only need to update value, you can do that like this,



             for($i=1; $i <= 5; $i++) {
          if(${'q' . $i}=='') ${'q' . $i}='X';
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 4 hours ago









          WhatatimetobealiveWhatatimetobealive

          676411




          676411













          • Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

            – Pedroski
            3 hours ago













          • Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

            – Whatatimetobealive
            2 hours ago



















          • Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

            – Pedroski
            3 hours ago













          • Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

            – Whatatimetobealive
            2 hours ago

















          Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

          – Pedroski
          3 hours ago







          Thanks! It occurred to me that I need to reassign the $qs at the end. Like this maybe: for($i=1; $i <= 5; $i++) { $answer= ${'q' . $i}; if($answer=='') $answer='X'; ${'q' . $i} = $answer; } Will that be OK like that??

          – Pedroski
          3 hours ago















          Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

          – Whatatimetobealive
          2 hours ago





          Oh yes I just converted the python code as you requested. if you wanna replace with X just replace them directly on if statement. I ll update my answer.

          – Whatatimetobealive
          2 hours ago













          3














          If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:



          $q1 = '';
          $q2 = 'A';
          $q3 = '';
          $q4 = 'E';
          $q5 = 'D';
          for ($i=1; $i <= 5; $i++) {
          if (${"q$i"} == '') ${"q$i"} = 'X';
          }
          echo "$q1n";
          echo "$q2n";
          echo "$q3n";
          echo "$q4n";
          echo "$q5n";


          Output:



          X
          A
          X
          E
          D


          Demo on 3v4l.org






          share|improve this answer


























          • So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

            – Pedroski
            3 hours ago











          • @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

            – Nick
            3 hours ago
















          3














          If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:



          $q1 = '';
          $q2 = 'A';
          $q3 = '';
          $q4 = 'E';
          $q5 = 'D';
          for ($i=1; $i <= 5; $i++) {
          if (${"q$i"} == '') ${"q$i"} = 'X';
          }
          echo "$q1n";
          echo "$q2n";
          echo "$q3n";
          echo "$q4n";
          echo "$q5n";


          Output:



          X
          A
          X
          E
          D


          Demo on 3v4l.org






          share|improve this answer


























          • So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

            – Pedroski
            3 hours ago











          • @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

            – Nick
            3 hours ago














          3












          3








          3







          If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:



          $q1 = '';
          $q2 = 'A';
          $q3 = '';
          $q4 = 'E';
          $q5 = 'D';
          for ($i=1; $i <= 5; $i++) {
          if (${"q$i"} == '') ${"q$i"} = 'X';
          }
          echo "$q1n";
          echo "$q2n";
          echo "$q3n";
          echo "$q4n";
          echo "$q5n";


          Output:



          X
          A
          X
          E
          D


          Demo on 3v4l.org






          share|improve this answer















          If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:



          $q1 = '';
          $q2 = 'A';
          $q3 = '';
          $q4 = 'E';
          $q5 = 'D';
          for ($i=1; $i <= 5; $i++) {
          if (${"q$i"} == '') ${"q$i"} = 'X';
          }
          echo "$q1n";
          echo "$q2n";
          echo "$q3n";
          echo "$q4n";
          echo "$q5n";


          Output:



          X
          A
          X
          E
          D


          Demo on 3v4l.org







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          NickNick

          33.2k132042




          33.2k132042













          • So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

            – Pedroski
            3 hours ago











          • @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

            – Nick
            3 hours ago



















          • So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

            – Pedroski
            3 hours ago











          • @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

            – Nick
            3 hours ago

















          So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

          – Pedroski
          3 hours ago





          So I don't need to reassign variables via $answer, just directly reassign ${"q$i"} = 'X'; That's great, I'll try it asap!

          – Pedroski
          3 hours ago













          @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

          – Nick
          3 hours ago





          @Pedroski no, no need to use an intermediate variable, you can do it all in one line.

          – Nick
          3 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54917530%2fhow-to-change-a-n-value-for-a-value-like-x%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...

          Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

          VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...