John Davidson

PHP: Unable to echo variable posted from python outside `if(isset($_POST['sign']))` block

0 comments
Message:


I have searched almost everywhere read different documents but still I am unable to find answer of question which is Why we can't use variable outside if(isset($_POST['sign'])) block which posted from python?
Consider this below code.


$sign = '';
if(isset($_POST['sign']))
{
$sign = $_POST['sign'];

if(!is_dir($sign))
{
mkdir($sign);
echo "folder created";
}
else
{
echo "folder already exists";
}
}
echo $sign;

Here I am getting value coming from python in $sign it is working fine inside if block but when I echo it outside if block it is showing noting on browser.



My Answer:
The issue you are facing is most likely due to the fact that the variable $sign is only being set inside the if(isset($_POST['sign'])) block. If the condition inside the if block is not met, then $sign will not be set and therefore will not be echoed outside of the block.

To solve this issue, you can initialize the $sign variable outside of the if block with a default value. This way, even if the condition inside the if block is not met, $sign will still have a value to be echoed outside of the block.

Here is an updated version of your code:

php
$sign = ''; // Initialize $sign variable with a default value

if(isset($_POST['sign']))
{
$sign = $_POST['sign'];

if(!is_dir($sign))
{
mkdir($sign);
echo "folder created";
}
else
{
echo "folder already exists";
}
}

echo $sign; // Echo $sign variable outside of the if block


By initializing $sign outside of the if block, you ensure that it will always have a value to be echoed, regardless of whether the condition inside the if block is met or not.

Rate this post

5 of 5 based on 2856 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us