php function echo - php

I have:
$an = "1989";
$luna = "4";
$zi = "23";
function CalzulareZodie($date){
list($an,$luna,$zi)=explode("-",$date);
if(($luna==1 && $day>20)||($month==2 && $zi<20)){
return "Varsator";
}else if(($luna==2 && $zi>18 )||($luna==3 && $zi<21)){
return "Pesti";
}else if(($luna==3 && $zi>20)||($luna==4 && $zi<21)){
return "Berbec";
}else if(($luna==4 && $zi>20)||($luna==5 && $zi<22)){
return "Taur";
}else if(($luna==5 && $zi>21)||($luna==6 && $zi<22)){
return "Gemeni";
}else if(($luna==6 && $zi>21)||($luna==7 && $zi<24)){
return "Rac";
}else if(($luna==7 && $zi>23)||($luna==8 && $zi<24)){
return "Leu";
}else if(($luna==8 && $zi>23)||($luna==9 && $zi<24)){
return "Fecioara";
}else if(($luna==9 && $zi>23)||($luna==10 && $zi<24)){
return "Balanta";
}else if(($luna==10 && $zi>23)||($luna==11 && $zi<23)){
return "Scorpion";
}else if(($luna==11 && $zi>22)||($luna==12 && $zi<23)){
return "Sagetator";
}else if(($luna==12 && $zi>22)||($luna==1 && $zi<21)){
return "Capricorn";
}
}
how can i echo the result of this function?
i've tried with:
$zodia=CalculareZodie();
echo "Zodia este: ".$zodia;
What is wrong?

Your function takes an argument. You are not passing one.
$zodia=CalculareZodie($somedate);
You've also misspelled it.

You forgot to pass argument. Also check the spelling CalzulareZodie
$zodia= CalzulareZodie('1989-4-23');
echo "Zodia este: ".$zodia;

You should pass the date as function's argument.
Also note that the variable declared outside the function are not available within the function. Either you have to declare them inside function or use global variable scope
$an = "1989";
$luna = "4";
$zi = "23";
function CalzulareZodie($date){
global $an, $luna, zi; // If you want
list($an,$luna,$zi)=explode("-",$date);
if(($luna==1 && $day>20)||($month==2 && $zi<20)){
return "Varsator";
}else if(($luna==2 && $zi>18 )||($luna==3 && $zi<21)){
return "Pesti";
}else if(($luna==3 && $zi>20)||($luna==4 && $zi<21)){
return "Berbec";
}else if(($luna==4 && $zi>20)||($luna==5 && $zi<22)){
return "Taur";
}else if(($luna==5 && $zi>21)||($luna==6 && $zi<22)){
return "Gemeni";
}else if(($luna==6 && $zi>21)||($luna==7 && $zi<24)){
return "Rac";
}else if(($luna==7 && $zi>23)||($luna==8 && $zi<24)){
return "Leu";
}else if(($luna==8 && $zi>23)||($luna==9 && $zi<24)){
return "Fecioara";
}else if(($luna==9 && $zi>23)||($luna==10 && $zi<24)){
return "Balanta";
}else if(($luna==10 && $zi>23)||($luna==11 && $zi<23)){
return "Scorpion";
}else if(($luna==11 && $zi>22)||($luna==12 && $zi<23)){
return "Sagetator";
}else if(($luna==12 && $zi>22)||($luna==1 && $zi<21)){
return "Capricorn";
}
}
and
$zodia=CalzulareZodie("Enter your date here");
echo "Zodia este: ".$zodia;

You need to pass the variables to the function, now, judging by the start of the function it takes a string with a date separated by dashes as an arguement so you'll need to do something like:
$an = "1989";
$luna = "4";
$zi = "23";
$date = $an . '-' . $luna . '-' . $zi; //Construct the string from the outside variables
Then you do:
$zodia=CalzulareZodie($date); //Pass constructed string to function.
echo "Zodia este: ".$zodia;

$zodia=CalculareZodie('specify date here');

There are a couple of things wrong.
You have a date argument which you are not passing in.
The variables are not accessible in the function and need to be set as global like this:
function CalzulareZodie($date){
global $an, $luna, $zi;
// REST OF THE FUNCTION
}

Related

How can make simpler this "IF"-condition

$regionFilter = isset($additionalDate['region']) &&
in_array($additionalDate['region'], $this->request["insuranceCompanyRegion"]);
$cityFilter = isset($additionalDate['city']) &&
in_array($additionalDate['city'], $this->request["insuranceCompanyCity"]);
if ($isRegionFilter && $isCityFilter) {
$filterCondition = $regionFilter && $cityFilter;
} elseif ($isCityFilter) {
$filterCondition = $cityFilter;
} elseif ($isRegionFilter) {
$filterCondition = $regionFilter;
}
if ($filterCondition) {
$this->companyIds[$q->id] = $q->name;
}
I need to make this condition simpler to edit in the future, how can I make it?
if ($regionFilter || $cityFilter) {
$this->companyIds[$q->id] = $q->name;
}
You can use shorthand If / Else check this 1
so for example
if($isRegionFilter && $isCityFilter){
$filterCondition = $regionFilter && $cityFilter;
becomes
$filterCondition=($isRegionFilter && $isCityFilter) ? $regionFilter && $cityFilter;
$filterCondition=($isRegionFilter && $isCityFilter) ? $regionFilter && $cityFilter : (($isRegionFilter) ? $regionFilter : $cityFilter);

undefined index but i can't understand why

Here is my code
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && $_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
Please help.
Thanks.
When you do && it will evaluate all conditions until something is false. When you do || it will evaluate all conditions until something is true. Since your first conditions evaluated to the false, the 2nd one was invoked but $_POST['error'] didn't exist.
You probably want to do this, notice the brackets around your two errors.
if(
isset($_POST['error']) &&
(
$_POST['error'] == 2 ||
$_POST['error'] == 1
)
)
It can also be better re-written as.
if(
isset($_POST['error']) &&
in_array($_POST['error'], array(1,2))
)
Like Augwa said:
The && operator will evaluate all conditions until any one of them is false.
The || operator will evaluate all conditions until any one of the is true.
A solution:
if(isset($_POST['error'])) {
if($_POST['error'] != 2 && $_POST['error'] !=1) {
// Do stuff here
return true;
}else if($_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
}
you should change your code as below... always enclose your comparison with || in brackets. because || condition checks up to last piece of code to find out a 'true' value. by re-coding as ..... && (... || .... ), the executions will return from the point && and will not execute ( .... || ..... ) part
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && ($_POST['error'] == 2 || $_POST['error'] == 1)) {
return false;
} else {
return false;
}

Function always returning true no matter input

I trying to create a function that matches the first number in a string to a state. For example if the user inputs a number that starts with 3, and the state 'vic', then the form should not present any errors. However no matter what is entered the function always returns true. Any help would be appreciated.
$state = array('Please Select', 'VIC', 'NSW', 'QLD', 'NT', 'WA', 'SA', 'TAS', 'ACT'); //In the form this is a drop down menu
$selected_key = $_POST['state'];
$postcode = $_POST["postcode"]; //The full number entered by the user
$errMsg .= validatePS($postcode, $selected_key);
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$post = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$post = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$post = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
if ($errMsg !=""){
echo "<p>Please correct the following errors...</p>"; //Prints out errors
echo "<p>$errMsg</p>";
}
You have two "flag" variables - $post which you are updating and $valid which you rely on. If you reduce them to one variable, you should get the behavior you want:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$valid = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$valid = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$valid = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
Note that you could clean up this code considerably by using logical operators instead of multiple if statements:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if ((($digit == 3) or ($digit == 8) && ($state == 'vic')) ||
(($digit == 1) or ($digit == 2) && ($state == 'nsw')) ||
(($digit == 4) or ($digit == 9) && ($state == 'qld'))) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}

Session Userdata Type not works in particular function in Codeigniter

I use this code in home function it get the usertype and works nice.
function home()
{
$type = $this->session->userdata('type');
if($type == "admin")
{
$this->load->view('index');
}
else if($type == "QA" || "SC" || "CDC")
{
$this->load->view('aindex');
}
}
But when I use same code in jobsheet function it's not working. Code below
function jobsheet()
{
$type = $this->session->userdata('type');
$this->load->model('Ipss_model');
$var1['job'] = $this->Ipss_model->AllJobSheet();
$var2['division'] = $this->Ipss_model->AllDivision();
$var3['tItem'] = $this->Ipss_model->transactionItem();
$data = $var1 + $var2 + $var3;
if($data['job'] != NULL )
{
if($type == "SC" || "CDC")
{
$this->load->view('jobsheet',$data);
}
else if($type == "admin" || "QA" )
{
$this->load->view('jobsheetQA',$data);
}
}
else
{
$this->load->view('jobsheetEmpty',$data);
}
}
Please help me about this. Thanks in advance.
To check weather array is empty or not you can use empty(). Also change your || condition like below.
So you condition would be
if(!empty($data))
{
if($type == "SC" || $type =="CDC")
{
$this->load->view('jobsheet',$data);
}
else if($type == "admin" || $type == "QA" )
{
$this->load->view('jobsheetQA',$data);
}
}
check your array() before using it in if condition
with -:
echo "<pre>";
print_r($data);
die();

Need help to add second condition to existing code

I am trying to add a second condition to existing code but it doesn't seem to be working.
The conditions are:
Compare two strings, from different arrays (working)
And check the value of a third string from a different array (not
working)
Here is the working code without the second condition: http://pastebin.com/bfpNb9zw
Here is my attempt:
Basically, the bit I am trying to get working is this part && ($ca = '') && ($ca = '0') && ($ca = '1') but it seems $ca is not able to be read outside the loop
if(!function_exists('lookup')){
function lookup($chain, $type) {
$cacount = count($chain['tbsCertificate']['extensions']);
for($j = 0; $j < $cacount; $j++) {
$count = count($chain['tbsCertificate'][$type]['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
$oid = array('id-at-commonName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(
array_key_exists($field, $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
in_array($chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
) {
$value = $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
return $value;
$ca = '';
if(isset($chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'])) {
$ca = $chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'];
}
}
}
}
}
return null;
}
}
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '1')) {
echo 'primary ca';
} else {
echo 'Root';
}
You are using =, which sets the value of $ca. You should be using === to check the value, instead.
Example:
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '1')) {
echo 'primary ca';
} else {
echo 'Root';
}

Categories