Checking if a variable is defined?

Hi
Is there a way to check if a variable is defined or not? In 2.9 it was possible to do:

:if ([:len $MyVar] = 0) do={ some code }

How to do the same in 3.x?

You don’t need to check if variable is defined, because you have to define each variable at the beginning of a script in v3.x

OK but i have to keep some information in var for my script (value is also written in script). When MT is restarted the variable isn’t defined and script cannot work.

:global aaa;
:if (:typeof[$aaa] = "nil") do={
      ...
}

but i need it for somewhat

in php

<? php
$offer=5000;
if ( ($var1 = $_GET["price"]) ) {;
	echo "your offer : $var1";
;} else {;
	$var1=$offer;
	echo "no price submited, using current offered ($var1)";
;};
?>

if i forgot to write :

/queue type add kind=pcq name="xSpd private DL" pcq-classifier=dst-address pcq-rate=2M

basically i need value of :

[ /queue type get [ find where name="xSpd private DL" ] pcq-rate ]

then assign that value into $var1,

however if i applied into mikrotik :

:local fullspeed "5000";
:if (	(:local var1 [ /queue type get [ find where name="xSpd private DL" ] pcq-rate ])	) do={ 
	/log info ( "speed = (" . $var1 . ") " );
;} else={;
	:local var1 = $fullspeed ;
	/log warning ( "speed = use our default value (" . $var1 . ") " ) ;
;};

it doesnt working then i expected, whats wrong ? any ideas ?

You are defining var1 in different scopes multiple times.

Code should be:

:local var1 [ /queue type get [ find where name="xSpd private DL" ] pcq-rate ];
:if ($var1 ....) do={
 ...
} else={
 :set var1 $fullspeed
}

sorry for late reply … i never thought it was already replied … thank u …

hmm …
yes its working … it should be …

my script also ( i think ) …


here i types via winbox terminal :

/queue type add kind=pcq name="xSpd private DL" pcq-classifier=dst-address pcq-rate=2M

so when i print to console :

:put  [ /queue type get [ find where name="xSpd private DL" ] pcq-rate ]

it will return : “2000000”

then i create script via winbox : system → script

:if (  (:len [ /queue type get [ find where name="xSpd private DL" ] pcq-rate ]) > 100 ) do={;
	/log warning ("YES" );
;} else={
	/log warning ("NO")
}

in log window : “YES
its working … YAY!!! :smiley:

BUT, when i :

/queue type remove "xSpd private DL"

then i run the script
where is /log warning (“NO”) go ? there is no “YES” or “NO”.

including what youve instructed sir, there is no error sign, there is no success sign



:local var1 [ /queue type get [ find where name="xSpd private DL" ] pcq-rate ];
:if ($var1 > 100) do={
	/log warning ( "YES" );
} else={
	/log warning ( "NO" )
}

now : is it a variable ?
if yes : how “Checking if a variable is defined?”
if no : how “tocheck rule was created?”
or : how can i check is error?
in php i just write : if ( isset ($whatVariable) ){ … ;}else{…;}; /or just/ if ( ($whatVariable) ){ … ;}else{…;};
or did i missed something important ? since that day, i cant go any further … its because i want to “add” or “set” (if a variable was defined) rules

ive RB450G MikroTik RouterOS 6.33.1 (c) 1999-2015, and there rest of myRB result still same.