im wanting to check whether a inputted triangle is either a isosceles, equal etc.. the first if statement works, but once i introduce the second one it says unexpected t_else, here is the code...
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
any ideas?
Maybe this is even better? ( Based on the example from Vlad Preda:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
switch ($uniqueSides) {
case 1:
return "Equilateral";
break;
case 2:
return "Isosceles";
break;
default:
return "Normal triangle";
break;
}
}
You have a syntax error:
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
Need a brace }
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
I would suggest a slightly different, more readable approach:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
if ($uniqueSides == 1) {
return "Equilateral";
} elseif ($uniqueSides == 2) {
return "Isosceles";
} else {
return "Normal triangle";
}
}
Related
I have an export to excel button which downloads excel file.
However when i click it shows me error as Trying to access array offset on value of type int
My code is this:
public static function dataTypeForValue($pValue = null)
{
// Match the value against a few data types
if ($pValue === null) {
return PHPExcel_Cell_DataType::TYPE_NULL;
} elseif ($pValue === '') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ($pValue instanceof PHPExcel_RichText) {
return PHPExcel_Cell_DataType::TYPE_INLINE;
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) { //error comes in this line
return PHPExcel_Cell_DataType::TYPE_FORMULA;
} elseif (is_bool($pValue)) {
return PHPExcel_Cell_DataType::TYPE_BOOL;
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
$tValue = ltrim($pValue, '+-');
if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
return PHPExcel_Cell_DataType::TYPE_STRING;
}
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
return PHPExcel_Cell_DataType::TYPE_ERROR;
}
return PHPExcel_Cell_DataType::TYPE_STRING;
}
}
what can be the solution for this..?
In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
(!is_int($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) should work. $pValue[0] will not work with integers so check if it is an int or not before continuing.
I update some files in library and It's work fine for me at php 7.4
Please check updated code library :
Google Drive LInk
I'm trying to repeat php "if" 20 times with dh_c_01_01. How I can do it?
if ($dh_c_01_01['id'] == $a_c_a_max) {
$dh_sq_01_01 = $a_b_s_05;
} elseif ($dh_c_01_01['id'] == $a_c_a_1_b) {
$dh_sq_01_01 = $a_b_s_06;
} elseif ($dh_c_01_01['id'] == $a_c_a_2_b) {
$dh_sq_01_01 = $a_b_s_07;
} elseif ($dh_c_01_01['id'] == $a_c_a_3_b) {
$dh_sq_01_01 = $a_b_s_08;
} elseif ($dh_c_01_01['id'] == $a_c_a_4_b) {
$dh_sq_01_01 = $a_b_s_09;
} elseif ($dh_c_01_01['id'] == $a_c_a_5_b) {
$dh_sq_01_01 = $a_b_s_10;
} elseif ($dh_c_01_01['id'] == $a_c_a_6_b) {
$dh_sq_01_01 = $a_b_s_11;
} elseif ($dh_c_01_01['id'] == $a_c_a_7_b) {
$dh_sq_01_01 = $a_b_s_12;
} elseif ($dh_c_01_01['id'] == $a_c_a_8_b) {
$dh_sq_01_01 = $a_b_s_13;
} elseif ($dh_c_01_01['id'] == $a_c_a_9_b) {
$dh_sq_01_01 = $a_b_s_14;
}
if ($dh_c_02_01['id'] == $a_c_a_max) {
$dh_sq_02_01 = $a_b_s_05;
} elseif ($dh_c_02_01['id'] == $a_c_a_1_b) {
$dh_sq_02_01 = $a_b_s_06;
} elseif ($dh_c_02_01['id'] == $a_c_a_2_b) {
$dh_sq_02_01 = $a_b_s_07;
} elseif ($dh_c_02_01['id'] == $a_c_a_3_b) {
$dh_sq_02_01 = $a_b_s_08;
} elseif ($dh_c_02_01['id'] == $a_c_a_4_b) {
$dh_sq_02_01 = $a_b_s_09;
} elseif ($dh_c_02_01['id'] == $a_c_a_5_b) {
$dh_sq_02_01 = $a_b_s_10;
} elseif ($dh_c_02_01['id'] == $a_c_a_6_b) {
$dh_sq_02_01 = $a_b_s_11;
} elseif ($dh_c_02_01['id'] == $a_c_a_7_b) {
$dh_sq_02_01 = $a_b_s_12;
} elseif ($dh_c_02_01['id'] == $a_c_a_8_b) {
$dh_sq_02_01 = $a_b_s_13;
} elseif ($dh_c_02_01['id'] == $a_c_a_9_b) {
$dh_sq_02_01 = $a_b_s_14;
}
[$dh_sq_03_01, ... , $dh_sq_20_01]
up to 20 times
I cannot be sure I understand your question correctly, or assume I know what your variables are or what their values are.
Based on your question, this example uses extra variables, for loops and dynamic variables. It also assumes that each of your dynamic variables exists:
<?php
$dh_c_limit = 20; // <-- loop limit
$a_c_a_limit = 9; // <-- loop limit
$a_b_s_addition = 5; // <-- based on your question
$a_c_a_max = 0; // <-- change this to whatever '$a_c_a_max' should be!
for ($dh_i = 1; $dh_i <= $dh_c_limit; $dh_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == $a_c_a_max){ // <-- checks against '$a_c_a_max'
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad($a_c_i,2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
}
else{
for ($a_c_i = 1; $a_c_i <= $a_c_a_limit; $a_c_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == ${"a_c_a_".$a_c_i."_b"}){ // <-- checks against '$a_c_a_' variable
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad(($a_c_i+$a_b_s_addition),2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
break; // <-- break the loop
}
}
}
}
?>
That is a bad habit. Try to use switch case, or use array as container
$array = [
'id1' => 'action1',
'id2' => 'action2',
...
];
$dh_sq_02_01 = $array[$dh_c_02_01['id']];
Just and example. Hope helps
PHP
$name = karlaxis;
if ($name == "") {
return false;
} elseif ($name != karlaxis) {
return false;
} else {
return true;
}
jQuery
$.post("#name-form").attr("action").serializearray(), function(result){
if (result == false) {
console.log("result of the IF CONDITION");
} elseif (result == false) { //the result of ELSEIF in php
console.log("result of the ELSEIF CONDITION");
} else {
console.log("result of the ELSE CONDITION");
}
})
Now, how to get the result of the elseif?
How to code that one?
If it is very important to get the result of different conditions, you can do a simple trick with your code. Rather than returning bool you may return int values like 0,1,2 where 0 is for IF, 1 is for ELSE IF, 2 is for ELSE.
php:
$name = karlaxis;
if($name == ""){
return 0;
}elseif($name != karlaxis){
return 1;
}else{
return 2;
}
jquery:
$.post("#name-form").attr("action").serializearray(), function(result){
IF(result == 0){
console.log("result of the IF CONDITION");
}ELSE IF(result == 1){ //the result of ELSEIF in php
console.log("result of the ELSEIF CONDITION");
}ELSE{
console.log("result of the ELSE CONDITION");
}
})
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();
I'm having some trouble where my last else statement never runs even though I know the first if will eventually return false, any ideas?
if ($productType == 'HSweat' || 'T Shirt') {
if ($productType == 'HSweat') {
if (!$queryResult) {
}
} elseif ($productType == 'T Shirt') {
if (!$queryResult) {
}
}
} else {
}
if ($productType == 'HSweat' || 'T Shirt') {
Should be
if ($productType == 'HSweat' || $productType == 'T Shirt') {
'T Shirt' as a string will always evaluate to True
I would recommend to use switch :
switch( $productType ){
case 'HSweat':
if (!$queryResult) {
//Do something
}
break;
case 'T Shirt':
if (!$queryResult) {
//Do something
}
break;
default:
//Do something
break;
}